【问题标题】:node how to run a Interactivity script( python, shell , etc..)节点如何运行交互脚本(python、shell 等)
【发布时间】:2014-05-02 15:54:00
【问题描述】:

我有一个 python 交互脚本,如下例:

$ python sync_email.py

please input your email: myemail@email.com

  validating...

please input your password: 

  password correct.

Now will sync the email to local...

我想用node运行python脚本,自动输入邮箱和密码。

实际上我想在无阻塞 IO 中使用不同的电子邮件批量执行 python 脚本。该怎么做?

【问题讨论】:

标签: node.js


【解决方案1】:

您的问题的答案可能是 Python-Shell, 使用 npm 安装。 https://www.npmjs.org/package/python-shell

向 Python 脚本发送数据(写入标准输入):

var PythonShell = require('python-shell'); 
var pyshell = new PythonShell('my_script.py');    
// sends a message to the Python script via stdin
pyshell.send('hello');

从 Python 脚本接收(只在 python 中使用 print):

var PythonShell = require('python-shell'); 
var pyshell = new PythonShell('my_script.py');

pyshell.on('message', function (message) { 
// received a message sent from the Python script (a simple "print" statement)  
console.log(message); });

非常易于使用,但请注意,您可能会指定脚本的参数/路径,因此您必须像这样使用它。

var options = {
  mode: 'text',
  scriptPath: '/absolute/path/to/my/scripts',
  args: ['param1', 'value1', 'param2', 'value2']
};
var pyshell = new PythonShell('my_script.py',options);

对于您的目的来说可能为时已晚,但它可能会帮助其他人,因为我遇到了同样的问题并且花了我一些时间;)

【讨论】:

  • 很好的答案。顺便说一句,我如何在我的 python 脚本中接收我的参数?
  • @QuikProBroNa 我通常会不断扫描 Stdin 以获取输入 -> 而 True:userinput = stdin.readline().rstrip('\n')
猜你喜欢
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
相关资源
最近更新 更多