【PyExecJS】

#encoding:utf-8#author:walker#date:2019-03-13#summary:利用PyExecJS调用js函数importexecjsJSCode=r'''functionadd(x,y){returnx+y;}'''CTX=execjs.compile(JSCode)deftest():#直接使用print(execjs.get().eval('3+2'))#调函数使用print(CTX.call('add',3,6))if__name__=='__main__':test()


【Node.js】

#encoding:utf-8#author:walker#date:2019-03-13#summary:直接用Node.js调用js函数fromsubprocessimportcheck_outputdeftest():#直接调用bytesTxt=check_output('node-econsole.log(3+2)',timeout=100)print(bytesTxt.decode('utf8').strip())#用node直接执行js脚本bytesTxt=check_output(['node','t.js','3','6'],timeout=100)print(bytesTxt.decode('utf8').strip())if__name__=='__main__':test()

t.js

functionadd(x,y){returnx+y;}varargs=process.argv.splice(2);console.log(add(parseInt(args[0]),parseInt(args[1])));


【Node.js 指定函数】

#encoding:utf-8#author:walker#date:2019-03-14#summary:直接用Node.js调用指定js函数fromsubprocessimportcheck_outputJSCode=r'''functionadd(x,y){returnx+y;}functionsub(x,y){returnx-y;}functionfoo(x){returnx;}'''deftest():jscode=JSCode+'process.stdout.write(add(3,2).toString())'rtn=check_output('node',input=jscode,universal_newlines=True,timeout=100)print(rtn)if__name__=='__main__':test()


【相关阅读】

Python之系统交互(subprocess)


*** walker ***