[英]Running Python script in nodejs
我正在嘗試使用node.js服務器運行python腳本
一個簡單的程序可以完美運行。 但是,當我嘗試使用python中的某些功能時,會引發錯誤。 例如。
我正在編寫一個程序來從用戶那里獲取輸入並回復相同的內容。
我正在python中使用raw_input ,但在node.js中不起作用。
誰能幫幫我嗎。
這是python代碼:
while True :
question=raw_input('you :')
print cb1.ask(question)
Node.js代碼:
var PythonShell = require('python-shell');
PythonShell.run('index.py', function (err, results) {
if (err) throw err;
console.log('result: %j', results);
});
PythonShell接受您可以通過此示例中的 options參數傳遞給python腳本的參數。
var PythonShell = require('python-shell');
var options = {
mode: 'text',
pythonPath: 'path/to/python',
pythonOptions: ['-u'],
scriptPath: 'path/to/my/scripts',
args: ['value1', 'value2', 'value3']
};
PythonShell.run('my_script.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
同時,在python腳本中,您可以訪問以下參數傳遞的參數:
import sys
arg1 = sys.argv[1] #value1
arg2 = sys.argv[2] #value2
arg3 = sys.argv[3] #value3
這是python腳本從命令行接受參數的方式。
至於您的問題,我認為如果您要接受來自node.js的輸入,則無需在python中使用raw_input
。 也就是說,如果您只是將python用於后台進程。
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.