Returning data from Python to node.js

Viewed 9751

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.

3 Answers
Related