python打开js文件_python怎么运行js文件?

5ec8bff683f3f855.jpg

1,首先通过,get_js方法,读取本地的 des_rsa.js 文件。

2,调用 execjs.compile() 编译并加载 js 文件内容。import execjs

#执行本地的js

def get_js():

# f = open("D:/WorkSpace/MyWorkSpace/jsdemo/js/des_rsa.js",'r',encoding='UTF-8')

f = open("./js/des_rsa.js", 'r', encoding='UTF-8')

line = f.readline()

htmlstr = ''

while line:

htmlstr = htmlstr + line

line = f.readline()

return htmlstr

jsstr = get_js()

ctx = execjs.compile(jsstr)

print(ctx.call('enString','123456'))

3,使用call()调用js中的方法,具体方法如下:function enString(data){

var key1 = "YHXWWLKJYXGS";

var key2 = "ZFCHHYXFL10C";

var key3 = "DES";

var enchex = strEnc(data,key1,key2,key3);

return enchex;

}