python 调⽤js 的⼏种⽅式
1.PyExecJS
安装依赖
pip3 install PyExecJS
新建add.js⽂件
py⽂件去调⽤
运⾏2.js2py
#安装依赖库
pip3 install js2py
绝世剑神林辰八一还是上⾯的add.js⽂件
调⽤
3.Node.js function add (a ,b ){ return a +b ;}
1
2
3import execjs with open ('add.js', 'r', encoding ='UTF-8') as f : js_code = f .read ()context = execjs pile (js_code )result = context .call ("add", 2, 3) // 参数⼀为函数名,参数⼆和三为函数的参数print (result )
1
免费素材软件2
3
4
5
67
如何输入vlookup函数import js2py with open ('add.js', 'r', encoding ='UTF-8') as f : js_code = f .read ()context = js2py .EvalJs ()context .execute (js_code )result = context .add ("1", "2")print (result )
1
2
3
4
5
6
7
实际上是使⽤ Python 的os.popen执⾏ node 命令,执⾏ JS 脚本⾸先,确保本地已经安装了 Node.js 环境
对js代码添加打印
⽤python调⽤控制台⽅式去使⽤
或者使⽤另⼀种⽅式
⽤node做⼀个服务,提供api
还是add.js⽂件
新建add_api.js
下载 express 和 body-parser 两个包function add (a ,b ){ return Number (a )+Number (b );}console .log (add (process .argv [2], process .argv [3])); // 运⾏脚本传进来的参数1
2
3
4import os nodejs = os .popen ('node add.js '+'2'+' '+'3')m = nodejs .read ()nodejs .close ()print (m )
1
2
3
4
5
6function add (a ,b ){ return Number (a )+Number (b );}// console.log(add(process.argv[2], process.argv[3]));//新增⼀个导出函数(node ⽅式)module .exports .init = function (arg1, arg2) { //
调⽤函数,并返回 console .log (add (arg1, arg2));};
1
2
3
4
5
6
7
python请求并解析json数据8
9
10import os cmd = 'node -e "require(\\"%s\\").init(%s,%s)"' % ('./add.js', 2, 3)pipeline = os .popen (cmd )result = pipeline .read ()print (result )
12
3
4
5function add (a ,b ){ return Number (a )+Number (b );}module .exports = { add : function (arg1, arg2) { return add (arg1, arg2); }};
1
2
3
4
5
6
7
8
9
运⾏,⽤python写个post请求var express = require ('express')var app = express ()var func = require ('./add.js') // 导⼊js 模块,并命名为func var bodyParser = require ('body-parser'); // 导⼊请求体解析器// 调整参数⼤⼩限制,否则会提⽰参数过⼤。app .use (bodyParser .urlencoded ({limit : '50mb', extended : true }));// 设置路由app .post ('/add', function (req , res ) { // 获取请求的真实IP var ip = req .headers ['x-real-ip'] ? req .headers ['x-real-ip'] : req .ip .replace (/::ffff:/, ''); // 获取请求时间 var time = new Date ().toString ().replace (/\+0800.*/, ''); // 打印请求时间、IP 、⽅法、路由 console .log ('INFO:', time , ip , req .method , req .originalUrl , '200 OK!'); // 获取POST 请求的formdata let result = req .body ; // let code = de; // let seed = result.seed; // let ts = resul
t.ts; console .log ("result: ", result ); console .log ("num1: ", result .num1); console .log ("num2: ", result .num2); // 调⽤cook 模块中的get_cookie ⽅法,该⽅法需要提前ports 导出 var response = func .add (result .num1, result .num2); // 设置响应头,如果不设置,通过asyncio_requests 请求的res.json()会报错,因为它是根据响应头解析json 数据 // ⽽requests 可以直接使⽤res.json()解析,因为它是根据响应信息解析 res .set ('Content-Type', 'application/json') // 将JSON 后的数据返回客户端 res .send (JSON .stringify ({"result": response }));});app .listen (8919, () => { console .log ("开启服务,端⼝8919", new Date ().toString ().replace (/\+0800.*/, ''))})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
在文本中写文字滚动代码24
25
26
27
28
29
30
31
32
33
34
35
36import requests response = requests .post ("127.0.0.1:8919/add", data ={"num1": 2, "num2": 3})print (response .text )1
2
acetate3
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论