如何实现nodejs中运行python脚本?

思路:在nodejs调用本地的cmd命令,通过cmd命令执行唤起python解析器,用python命令去执行python脚本;

node的参考链接:

http://nodejs.cn/api/child_process.html

child_process模块有两个方法。分别是execexecSync,分别表示异步和同步,

 

异步实现:

const pro = require("child_process")

pro.exec("python url.py", function (error, stdout, stderr) {
    if (error) {
        console.info("stderr:" + stderr)
    }
    console.log("exec:" + stdout)
})
 
同步实现:
const py = pro.execSync("python url.py")
console.log(py.toString())
 

这样就实现了Nodejs与Python脚本的交互

posted @ 2021-11-30 16:03  墨染清浅  阅读(2277)  评论(0)    收藏  举报