Node.js Communicate With Python-shell
I have the json data in nodejs. And I try to pass this data to python. But I can't get a reply from PythonFIle. jsonData Format [ { id: 123, option: ['A','B'],
Solution 1:
First of all, you can not send a JSON variable by send() method, because this method sends to stdin which accepts only string and bytes. Please try to execute my example:
test.py
value = input()
print(f"Python script response: {value}")
test.js
const {PythonShell} = require('python-shell')
const pyshell = newPythonShell('test.py');
pyshell.send(JSON.stringify({"hello": "hello"}));
pyshell.on('message', function (message) {
console.log(message);
});
pyshell.end(function (err,code,signal) {
if (err) throw err;
console.log('finished');
});
If this example works for you, then change {"hello": "hello"}
to jsonData
.
I hope that I helped you.
Post a Comment for "Node.js Communicate With Python-shell"