python调用js的几种方式

下面介绍几个常用的运行js的几种方式
1.PyExecJS
安装依赖

pip install PyExecJS

存储add.js文件

function add(a,b){
    return a+b;
}

pyhton去调用

import execjs

with open('add.js', 'r', encoding='UTF-8') as f:
    js_code = f.read()
context = execjs.compile(js_code)
result = context.call("add", 2, 3) // 参数一为函数名,参数二和三为函数的参数
print(result)

2.js2py 安装依赖库

pip install js2py

python去调用

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)

3.用 Node
添加add.js

function add(a,b){
    return Number(a)+Number(b);
}
console.log(add(process.argv[2], process.argv[3]));  // 运行脚本传进来的参数

用python调用控制台方式去使用

import os
nodejs = os.popen('node add.js '+'2'+' '+'3')
m = nodejs.read()
nodejs.close()
print(m)

或者使用另一种方式

function 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));
};
复制代码
import os
cmd = 'node -e "require(\"%s\").init(%s,%s)"' % ('./add.js', 2, 3)
pipeline = os.popen(cmd)
result = pipeline.read()
print(result)

4.node服务

function add(a,b){
    return Number(a)+Number(b);
}

module.exports =  {
    add: function (arg1, arg2) {
        return add(arg1, arg2);
    }
};

下载 express 和 body-parser 两个包

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 = result.code;
    // let seed = result.seed;
    // let ts = result.ts;
    console.log("result: ", result);
	console.log("num1: ", result.num1);
	console.log("num2: ", result.num2);

	// 调用cook模块中的get_cookie方法,该方法需要提前module.exports导出
	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.*/, ''))
})

运行,用python写个post请求

import requests
response = requests.post("http://127.0.0.1:8919/add", data={"num1": 2, "num2": 3})
print(response.text)

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值