python使用nodejs执行js代码

import execjs
import os
os.environ["NODE_PATH"]=r"C:\nodejs\node_global"
#查看当前运行环境,有问题可在环境变量path中新添加%NODE_PATH%尝试下
print(execjs.get().name)
with open('test.js','r',encoding='utf-8') as f:
    js=f.read()

JS=execjs.compile(js)
sign=JS.call('func','微信')
print(sign)

或者安装pyndjs库

from pyndjs import execute

js_code="""
    function cnDeCryptV2(str) {
        var k = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", l = k.length, b, b0, b1, b2, b3, d = 0, s;
        s = new Array(Math.floor(str.length / 4)),
        b = s.length;
        for (var i = 0; i < b; i++)
            b0 = k.indexOf(str.charAt(d)),
            d++,
            b1 = k.indexOf(str.charAt(d)),
            d++,
            b2 = k.indexOf(str.charAt(d)),
            d++,
            b3 = k.indexOf(str.charAt(d)),
            d++,
            s[i] = ((b1 + b0 * l) * l + b2) * l + b3;
        b = eval("String.fromCharCode(" + s.join(",") + ")");
        var w = "";
        return b.split("|").forEach((function(e, t) {
            t > 0 && (-1 != e.search(/【(.*?)】/) ? w += e.replace("【", "").replace("】", "") : e.length > 0 && (w += "&#x" + e + ";"))
        }
        )),
        w
    }
    """
"""
execute参数说明
js_code   js代码
fun_name   要执行的函数名
args   参数以列表形式传入
node_path   nodejs路径,可以自定义
encoding   编码格式,默认utf-8
"""
print(execute(js_code,"cnDeCryptV2",["001H001I001I001H001H001G002V001H001G003B0036001H003E001G002T001G003G09HS0LQD09HT003G002R002P002P001I003G002R002T001F002Q003G002R002S001D001G"]))

你可能感兴趣的:(javascript,前端,开发语言)