#需安装execJS pip install PyExecJS
#调用js进行base64加解密操作
import execjs
import base64
def encode(input):with open(r"Base64.js", 'r') as f:content = f.read() f.close() #编译并加载js ctx = execjs.compile(content)  #js方法jscode = 'base64Encode("{0}")'.format(input)#运行js的方法,返回该js方法的返回值res = ctx.eval(jscode)       return res
def decode(input):with open(r"Base64.js", 'r') as f:content = f.read() f.close()   ctx = execjs.compile(content)  #运行js的方法jscode = 'base64Decode("{0}")'.format(input)res = ctx.eval(jscode)       return res
if __name__=='__main__':  str = "123456abcd"enc = encode(str)res = decode(enc)    print(res)#输出结果:123456abcd#python的base64解码print(base64.b64decode(enc))#输出结果:b'123456abcd'
//Base64.js
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory(global):typeof define==="function"&&define.amd?define(factory):factory(global)})(typeof self!=="undefined"?self:typeof window!=="undefined"?window:typeof global!=="undefined"?global:this,function(global){"use strict";global=global||{};var _Base64=global.Base64;var version="2.5.1";var buffer;if(typeof module!=="undefined"&&module.exports){try{buffer=eval("require('buffer').Buffer")}catch(err){buffer=undefined}}var b64chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var b64tab=function(bin){var t={};for(var i=0,l=bin.length;i<l;i++)t[bin.charAt(i)]=i;return t}(b64chars);var fromCharCode=String.fromCharCode;var cb_utob=function(c){if(c.length<2){var cc=c.charCodeAt(0);return cc<128?c:cc<2048?fromCharCode(192|cc>>>6)+fromCharCode(128|cc&63):fromCharCode(224|cc>>>12&15)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}else{var cc=65536+(c.charCodeAt(0)-55296)*1024+(c.charCodeAt(1)-56320);return fromCharCode(240|cc>>>18&7)+fromCharCode(128|cc>>>12&63)+fromCharCode(128|cc>>>6&63)+fromCharCode(128|cc&63)}};var re_utob=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;var utob=function(u){return u.replace(re_utob,cb_utob)};var cb_encode=function(ccc){var padlen=[0,2,1][ccc.length%3],ord=ccc.charCodeAt(0)<<16|(ccc.length>1?ccc.charCodeAt(1):0)<<8|(ccc.length>2?ccc.charCodeAt(2):0),chars=[b64chars.charAt(ord>>>18),b64chars.charAt(ord>>>12&63),padlen>=2?"=":b64chars.charAt(ord>>>6&63),padlen>=1?"=":b64chars.charAt(ord&63)];return chars.join("")};var btoa=global.btoa?function(b){return global.btoa(b)}:function(b){return b.replace(/[\s\S]{1,3}/g,cb_encode)};var _encode=buffer?buffer.from&&Uint8Array&&buffer.from!==Uint8Array.from?function(u){return(u.constructor===buffer.constructor?u:buffer.from(u)).toString("base64")}:function(u){return(u.constructor===buffer.constructor?u:new buffer(u)).toString("base64")}:function(u){return btoa(utob(u))};var encode=function(u,urisafe){return!urisafe?_encode(String(u)):_encode(String(u)).replace(/[+\/]/g,function(m0){return m0=="+"?"-":"_"}).replace(/=/g,"")};var encodeURI=function(u){return encode(u,true)};var re_btou=new RegExp(["[À-ß][-¿]","[à-ï][-¿]{2}","[ð-÷][-¿]{3}"].join("|"),"g");var cb_btou=function(cccc){switch(cccc.length){case 4:var cp=(7&cccc.charCodeAt(0))<<18|(63&cccc.charCodeAt(1))<<12|(63&cccc.charCodeAt(2))<<6|63&cccc.charCodeAt(3),offset=cp-65536;return fromCharCode((offset>>>10)+55296)+fromCharCode((offset&1023)+56320);case 3:return fromCharCode((15&cccc.charCodeAt(0))<<12|(63&cccc.charCodeAt(1))<<6|63&cccc.charCodeAt(2));default:return fromCharCode((31&cccc.charCodeAt(0))<<6|63&cccc.charCodeAt(1))}};var btou=function(b){return b.replace(re_btou,cb_btou)};var cb_decode=function(cccc){var len=cccc.length,padlen=len%4,n=(len>0?b64tab[cccc.charAt(0)]<<18:0)|(len>1?b64tab[cccc.charAt(1)]<<12:0)|(len>2?b64tab[cccc.charAt(2)]<<6:0)|(len>3?b64tab[cccc.charAt(3)]:0),chars=[fromCharCode(n>>>16),fromCharCode(n>>>8&255),fromCharCode(n&255)];chars.length-=[0,0,2,1][padlen];return chars.join("")};var _atob=global.atob?function(a){return global.atob(a)}:function(a){return a.replace(/\S{1,4}/g,cb_decode)};var atob=function(a){return _atob(String(a).replace(/[^A-Za-z0-9\+\/]/g,""))};var _decode=buffer?buffer.from&&Uint8Array&&buffer.from!==Uint8Array.from?function(a){return(a.constructor===buffer.constructor?a:buffer.from(a,"base64")).toString()}:function(a){return(a.constructor===buffer.constructor?a:new buffer(a,"base64")).toString()}:function(a){return btou(_atob(a))};var decode=function(a){return _decode(String(a).replace(/[-_]/g,function(m0){return m0=="-"?"+":"/"}).replace(/[^A-Za-z0-9\+\/]/g,""))};var noConflict=function(){var Base64=global.Base64;global.Base64=_Base64;return Base64};global.Base64={VERSION:version,atob:atob,btoa:btoa,fromBase64:decode,toBase64:encode,utob:utob,encode:encode,encodeURI:encodeURI,btou:btou,decode:decode,noConflict:noConflict,__buffer__:buffer};if(typeof Object.defineProperty==="function"){var noEnum=function(v){return{value:v,enumerable:false,writable:true,configurable:true}};global.Base64.extendString=function(){Object.defineProperty(String.prototype,"fromBase64",noEnum(function(){return decode(this)}));Object.defineProperty(String.prototype,"toBase64",noEnum(function(urisafe){return encode(this,urisafe)}));Object.defineProperty(String.prototype,"toBase64URI",noEnum(function(){return encode(this,true)}))}}if(global["Meteor"]){Base64=global.Base64}if(typeof module!=="undefined"&&module.exports){module.exports.Base64=global.Base64}else if(typeof define==="function"&&define.amd){define([],function(){return global.Base64})}return{Base64:global.Base64}});
function base64Encode(input) {return Base64.encode(input);
}
function base64Decode(input) {return Base64.decode(input);
}

如果windows环境运行存在gbk编码报错问题,可以去python的Lib库找subprocess.py,586初始化函数的参数:encoding=None改成:encoding="utf-8"

python使用execJS运行js函数例子相关推荐

  1. execjs回调python_python使用execJS运行js函数例子

    //Base64.js (function(global,factory){typeof exports==="object"&&typeof module!==& ...

  2. python通过execjs运行js代码

    python通过execjs运行js代码 pip install PyExecJS 使用方法 import execjsctx = execjs.compile(""" ...

  3. python调用函数示例_Python使用execjs运行JS函数示例,python,execJS,例子

    //Base64.js (function(global,factory){typeof exports==="object"&&typeof module!==& ...

  4. 潇洒郎:Python 使用pyexejs运行js代码

    潇洒郎:Python 使用pyexejs运行js代码 安装 pip install pyexecjs js代码: import execjsjs_code='''function x(a, b){re ...

  5. python execjs详解_Python基于execjs运行js过程解析

    execjs 使用 有了selenium+Chrome Headless 加载页面为什么还要用execjs来运行js? selenium+Chrome Headless 必然是爬虫的一大利器,可是缺点 ...

  6. [284]python使用execjs执行js

    文章目录 1. 安装 2. 简单使用 3. 调用函数 4. 执行JS的环境 5.环境切换 6. 简易性能分析 1. 安装 pip install PyExecJS # 需要注意, 包的名称:PyExe ...

  7. python爬虫执行js代码_爬虫之python3用execjs执行JS代码

    JS运行环境 Node.js 是Javascript语言服务器端运行环境 测试运行 测试运行JavaScript程序 微软windows操作系统:附件-命令提示符 需进入到js程序的目录下,node命 ...

  8. python不弹窗执行js_python使用execjs执行js

    python 执行js代码 简单调用 importexecjs execjs.eval("new Date") 返回值为:2018-04-04T12:53:17.759Z exec ...

  9. python爬虫遇到js加密,python爬虫遇到JS加密,pyv8运行js,详细解密过程

    首先说一下什么是加密,所谓js加密大多出现在表单提交过程中,下面我将以中国电信为例,详细讲解如何利用pyv8来加密登录的密码.要说明的是pyv8目前仅仅支持python2,用的下伙伴要注意自己的pyt ...

最新文章

  1. RadASM的测试工程!
  2. HTTP中Accept与Content-Type区别
  3. GitHub最火热的30个开源机器学习框架
  4. 陕西小学三年级计算机下册教案,小学三三年级信息技术下册教学计划
  5. excel行列互换_Excel如何实现行列数据互换?其实除了复制粘贴,还能这样操作...
  6. 2017年11月01日普及组 I Like Matrix!
  7. Wave-Share -无服务器,点对点,通过声音共享本地文件
  8. HTTP基本认证(Basic Authentication)的JAVA示例
  9. 管桩的弹性模量计算公式_桩基设计计算公式
  10. springboot 多了8小时_日本人不明白:中国的奶茶有多好喝,值得排队8小时去买?...
  11. 【图论】拉普拉斯矩阵(Laplacian matrix)
  12. 支持!解决卡巴斯基程序错误及程序断开的问题!
  13. paip.Adblock屏蔽onlinedown华军软件园的4秒下载广告总结..
  14. J2EE框架DDoS漏洞预警公告
  15. 安全基础:0-day漏洞
  16. 杭州地铁行业十四五发展可行性及投资机遇研究报告2022版
  17. 28个Java开发常用规范技巧总结
  18. 苹果 2019 卖什么?新 iPhone 值不值得买?
  19. Unity场景素材导出为 FBX文件的方法
  20. 用matlab实现任意点图片的旋转_(实验二) --- 图像旋转变换---matlab实现

热门文章

  1. 程序员思维修炼:如何从新手成为专家
  2. 传说中的BNET边缘传输
  3. 3D角色遮挡住UI的实现,关于Stencil Buffer,描边,以及其他
  4. c 语言printf输出,位数不够补0
  5. STM32理论 —— DAC、DMA
  6. 玩转OneNET物联网平台之简介
  7. 阿里巴巴java开发手册(泰山版)在线阅览
  8. java分页类Page
  9. 配置Log4j(很详细)
  10. MMKV数据存储组件的使用介绍