简体   繁体   English

在运行时与nodejs应用程序内的python脚本进行交互

[英]Interact with python script inside nodejs application at runtime

Hello I'm trying to interact with a python script inside a nodejs application at runtime. 您好,我正在尝试在运行时与nodejs应用程序内的python脚本进行交互。

The python script is more a command center for doing whatsapp operations called yowsup. python脚本更是一个命令中心,用于执行称为yowsup的whatsapp操作。 https://github.com/tgalal/yowsup/tree/master https://github.com/tgalal/yowsup/tree/master

I'm able to run the 'Yowsup Cli client' in a shell and work with it. 我可以在外壳中运行“ Yowsup Cli客户端”并使用它。 But I want to run it in a nodejs application because it is written in python and I'm not good in python. 但是我想在nodejs应用程序中运行它,因为它是用python编写的,而我在python中却不太好。

So what I did was to spawn the command I normally use in the shell like this: 因此,我要做的是生成通常在shell中使用的命令,如下所示:

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. python脚本通常在开始时输出一些输出,但是在进程运行时我无法在node内得到任何东西。

I looked inside the python script and saw that the output is generated like this: 我查看了python脚本内部,发现生成的输出是这样的:

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

How can I get some data from the python script on runtime? 如何在运行时从python脚本中获取一些数据?

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): 这与您的方法稍有不同,并且使用了npm lib,但是可以工作( resultsrandom_geo.py脚本的标准输出):

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);
    }
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM