尝试设置一个调用Python脚本的Node.Js服务器

我正在尝试设置一个简单的Node.Js服务器,它在打一个url时调用一个Python脚本。 下面给出的是python和Node js服务器文件。

当我打到服务器的url。 页面加载! 但然后服务器崩溃,并给我以下错误(在cmd提示符):

Server listening on: http://localhost:8080/ this is here so we are in events.js:141 throw er; // Unhandled 'error' event ^ Error: spawn C:UsersShubhamAnaconda3libos.py ENOENT at exports._errnoException (util.js:837:11) at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32) at onErrorNT (internal/child_process.js:344:16) at doNTCallback2 (node.js:429:9) at process._tickCallback (node.js:343:17) 

这在Web浏览器控制台中:

 GET http://localhost:8080/favicon.ico net::ERR_CONNECTION_REFUSED 

我查了类似的问题。 但是他们有点不同,他们的修复不能帮助我。 我已经清除了高速caching并进行了检查。

Python文件:

 import sys def runForFun(artist, song, lyrics): if lyrics is None: print("artist:" + artist) print("song:"+song) print("lyrics:"+lyrics) theme = "theme" return theme else : print("lyrics: "+lyrics) try: runForFun(sys.argv[0], sys.argv[1], sys.argv[2]) except IndexError: print('Please supply arguments') 

节点js文件

 //Lets require/import the HTTP module var http = require('http'); var PythonShell = require('python-shell'); //Lets define a port we want to listen to const PORT=8080; //We need a function which handles requests and send response function handleRequest(request, response){ var options = { mode: 'text', pythonPath: 'C:\Users\Shubham\Anaconda3\lib\os.py', pythonOptions: ['-u'], scriptPath: 'C:\Users\Shubham\Google Drive\Capstone\Theme Extraction', args: ['value1', 'value2', 'value3'] }; console.log("this is here so we are in"); PythonShell.run('runPython.py', options, function (err, results) { if (err) throw err; console.log('results: %j', results, 'finished'); }); response.end('It Works!! Path Hit: ' + request.url); } //Create a server var server = http.createServer(handleRequest); //Lets start our server server.listen(PORT, function(){ //Callback triggered when server is successfully listening. Hurray! console.log("Server listening on: http://localhost:%s/", PORT); }); 

你应该在文件path中pythonPath斜杠,我认为pythonPath应该指向一个Python可执行文件,而不是脚本。

因此,正确的设置将如下所示

 var options = { mode: 'text', pythonPath: 'C:\\Python\\pythonw.exe', pythonOptions: ['-u'], scriptPath: 'C:\\Users\\Shubham\\Google Drive\\Capstone\\Theme Extraction', args: ['value1', 'value2', 'value3'] }; 

总的来说,除非你特别需要Node.js,为什么不使用一个Python web框架,你可以简单地导入你的脚本来运行呢?