如何将Python连接到Node.js?

问题描述 投票:0回答:1

我想向python发送一个node.js变量,用python修改它并将其重新发送到node.js,这是我的代码示例:Python

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

现在js代码:

var variable=prompt("enter the variable");
#after the code was sended to thd python code, modified and resend:
document.write(variable)

如何在python和javascript之间传播变量?

javascript python variables flask
1个回答
1
投票

这是一种方法,通过从节点内生成子python进程。

test.js:

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

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

有人script.朋友

import sys

def change_var(x):
    print('hello from python: {:d}'.format(int(x) + 2))

change_var(sys.argv[1])

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

stdout: hello from python: 4

目录布局

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

0 directories, 2 files

有关:

© www.soinside.com 2019 - 2024. All rights reserved.