Python 如何从 node.js 调用外部脚本/程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20972788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to invoke external scripts/programs from node.js
提问by alh
I have a C++program and a Pythonscript that I want to incorporate into my node.jsweb app.
我有一个C++程序和一个Python脚本,我想将其合并到我的node.jsWeb 应用程序中。
I want to use them to parse the files that are uploaded to my site; it may take a few seconds to process, so I would avoid to block the app as well.
我想用它们来解析上传到我网站的文件;处理可能需要几秒钟,所以我也会避免阻止该应用程序。
How can I just accept the file then just run the C++program and script in a sub-process from a node.jscontroller?
我怎样才能只接受文件然后C++在node.js控制器的子进程中运行程序和脚本?
采纳答案by Plato
see child_process. here is an example using spawn, which allows you to write to stdin and read from stderr/stdout as data is output. If you have no need to write to stdin and you can handle all output when the process completes, child_process.execoffers a slightly shorter syntax to execute a command.
见child_process。这是一个使用 的示例spawn,它允许您在输出数据时写入 stdin 并从 stderr/stdout 读取。如果您不需要写入 stdin 并且可以在进程完成时处理所有输出,则child_process.exec提供稍微短一些的语法来执行命令。
// with express 3.x
var express = require('express');
var app = express();
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(app.router);
app.post('/upload', function(req, res){
if(req.files.myUpload){
var python = require('child_process').spawn(
'python',
// second argument is array of parameters, e.g.:
["/home/me/pythonScript.py"
, req.files.myUpload.path
, req.files.myUpload.type]
);
var output = "";
python.stdout.on('data', function(data){ output += data });
python.on('close', function(code){
if (code !== 0) {
return res.send(500, code);
}
return res.send(200, output);
});
} else { res.send(500, 'No file found') }
});
require('http').createServer(app).listen(3000, function(){
console.log('Listening on 3000');
});
回答by Tejus Prasad
Might be a old question but some of these references will provide more details and different ways of including python in NodeJS.
可能是一个老问题,但其中一些参考资料将提供更多细节和在 NodeJS 中包含 python 的不同方法。
There are multiple ways of doing this.
有多种方法可以做到这一点。
- first way is by doing
npm install python-shell
- 第一种方法是做
npm install python-shell
and here's the code
这是代码
var PythonShell = require('python-shell');
//you can use error handling to see if there are any errors
PythonShell.run('my_script.py', options, function (err, results) {
//your code
you can send a message to python shell using
pyshell.send('hello');
您可以使用
pyshell.send('hello');
you can find the API reference here- https://github.com/extrabacon/python-shell
你可以在这里找到 API 参考 - https://github.com/extrabacon/python-shell
second way - another package you can refer to is node python , you have to do
npm install node-pythonthird way - you can refer to this question where you can find an example of using a child process- How to invoke external scripts/programs from node.js
第二种方式 - 你可以参考的另一个包是 node python ,你必须这样做
npm install node-python第三种方式 - 您可以参考这个问题,您可以在其中找到使用子进程的示例 - How to invoke external scripts/programs from node.js
a few more references - https://www.npmjs.com/package/python
还有一些参考资料 - https://www.npmjs.com/package/python
if you want to use service-oriented architecture - http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/
如果你想使用面向服务的架构 - http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

