Returning data from Python to node.js
node.js, python
Solution
I figured out how to do this, by printing the data in Python, and "listening" for the print in Node using stdout.on().
test.js
var sys = require('sys'),
spawn = require('child_process').spawn,
dummy = spawn('python', ['test.py']);
dummy.stdout.on('data', function(data) {
sys.print(data.toString());
});
test.py
import time
def dummy() :
out = '';
for i in range(0,10) :
out += str(i + 1) + ", "
time.sleep(0.1)
print out
if __name__ =='__main__' :
dummy = dummy()
Problem
I've been following along with a couple examples I found to call a Python script from Node. I'm able to execute the script, but I can't return data from Python. test.js ``` var sys = require('sys'), spawn = require('child_process').spawn, dummy = spawn('python', ['test.py']); dummy.stdout.on('data', function (data) { sys.print("testing...\n"); sys.print(data); }); ``` test.py ``` import time def dummy() : out = ''; for i in range(0,10) : out += str(i + 1) + "\n" time.sleep(0.1) print out return out if __name__ =='__main__' : dummy = dummy() ``` Could someone provide an example of how to return the results from test.py to test.js? Thanks. Note: test.py edited for proper indent.