python的execjs执行js代码

安装

# python3安装
pip install PyExecJS
# python2安装
pip install pyexecjs

execjs执行语法

import execjs

jsFunc = '''
    function add(x,y){
    return x+y;
    }
'''
jscontext = execjs.compile(jsFunc)
a = jscontext.call('add',3,5)
print(a)
# 可识别字符串,元组,字典,列表等

python中调用js文件使用js方法

1,首先通过,get_js方法,读取本地的 des_rsa.js 文件。2,调用 execjs.compile() 编译并加载 js 文件内容。3,使用call()调用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'))

你可能感兴趣的:(python的execjs执行js代码)