Interact with python script inside nodejs application at runtime

530 Views Asked by At

Hello I'm trying to interact with a python script inside a nodejs application at runtime.

The python script is more a command center for doing whatsapp operations called yowsup. https://github.com/tgalal/yowsup/tree/master

I'm able to run the 'Yowsup Cli client' in a shell and work with it. But I want to run it in a nodejs application because it is written in python and I'm not good in python.

So what I did was to spawn the command I normally use in the shell like this:

var spawn = require('child_process').spawn,
    ls    = spawn('./yowsup/yowsup-cli', ['demos','--login', '49XXXXXXXXXXX:8bF0hUewVcX1hf6adpuasonFdEP=', '--yowsup', '-E', 's40']);

ls.stdout.on('data', function (data) {
    console.log('stdout: ' + data.toString());
});

ls.stderr.on('data', function (data) {
    console.log('stderr: ' + data.toString());
});

ls.on('exit', function (code) {
    console.log('child process exited with code ' + code.toString());
});

The problem is, that I don't get any data from the process. The python script normally prints some output as start but I can't get anything inside node while the process is running.

I looked inside the python script and saw that the output is generated like this:

print("%s send '%s'" % (messageProtocolEntity.getFrom(False), messageProtocolEntity.getBody()))

How can I get some data from the python script on runtime?

1

There are 1 best solutions below

2
Chase On BEST ANSWER

This is slightly different than your approach and uses an npm lib, but does work (results is the stdout of the random_geo.py script):

var py = require('python-shell');
var pyOptions = {
  mode: 'text',
  pythonPath: '/opt/local/bin/python',
  scriptPath: '.'
};

function getCoords(req, res, next) {
  py.run('random_geo.py', pyOptions, function(err, results) {
    if (err) {
      console.log(err);
    } else {
      console.log(results);
    }
  });
}