Python3调用js函数-创新互联

【PyExecJS】

成都创新互联专业为企业提供常熟网站建设、常熟做网站、常熟网站设计、常熟网站制作等企业网站建设、网页设计与制作、常熟企业网站模板建站服务,十年常熟做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。#encoding: utf-8 #author: walker # date: 2019-03-13 # summary: 利用 PyExecJS 调用 js 函数 import execjs JSCode = r'''             function add(x, y) {                 return x + y;             } ''' CTX = execjs.compile(JSCode) def test():     # 直接使用     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 函数 from subprocess import check_output def test():     # 直接调用     bytesTxt = check_output('node -e console.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

function add(x, y) {     return x + y; } var args = 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 函数 from subprocess import check_output JSCode = r'''             function add(x, y) {                 return x + y;             }                          function sub(x, y) {                 return x - y;             }                          function foo(x) {                 return x;             } ''' def test():     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 ***

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:Python3调用js函数-创新互联
分享链接:http://pwwzsj.com/article/deoich.html