gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 23:52:53 25 4
gpt4 key购买 nike

我正在尝试设置一个简单的 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

我检查过类似的问题。但它们有点不同,它们的修复对我没有帮助。我已经清除了缓存并进行了检查。

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')

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

最佳答案

您应该转义文件路径中的斜杠,我认为 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 框架呢?

关于python - 尝试设置调用 Python 脚本的 Node.Js 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36700575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com