python 3 调用函数-Python3 调用 js 函数

#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.jsfunction 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()

【相关阅读】

***walker ***

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值