如何将Python连接到Node.js?

2024-06-20 15:15:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想发送给python a节点.js变量,用python修改它并重新发送到节点.js,这是我的代码示例: Python

def change_input(variable):
    variable*=5
    #now send variable to node.js

现在,js代码:

^{pr2}$

如何在python和javascript之间循环变量?在


Tags: to代码sendnode示例input节点def
1条回答
网友
1楼 · 发布于 2024-06-20 15:15:15

这里有一种方法,通过在节点内生成一个子python进程来实现。在

在测试.js公司名称:

const { spawn } = require('child_process');
const p = spawn('python', ['yourscript.py', '1']);

p.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

在你的脚本.py在

^{pr2}$

运行node ./test.js的输出:

stdout: hello from python: 4

目录布局

➜  test tree
.
├── test.js
└── yourscript.py

0 directories, 2 files

相关:

相关问题 更多 >