0

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?

Kingalione
  • 4,237
  • 6
  • 49
  • 84

1 Answers1

0

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);
    }
  });
}
Chase
  • 3,009
  • 3
  • 17
  • 23
  • when I try this on the yowsup-cli script I get errors like this: Error: process exited with code 1 at terminateIfNeeded – Kingalione Jun 24 '17 at 12:50
  • Someone else had your exact same issue - https://github.com/extrabacon/python-shell/issues/46 It's an error in the Python script. – Chase Jun 24 '17 at 18:39