温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python3 调用 js 函数

发布时间:2020-07-24 17:57:39 来源:网络 阅读:2405 作者:RQSLT 栏目:编程语言

【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 ***

向AI问一下细节
推荐阅读:
  1. Python3 函数
  2. Python3函数

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI