从Windows迁移到Ubuntu以来,我已经使node.js服务器使用python-shell 运行python脚本,并且遇到了EACCES错误。据我所知,并且能力有限,试图将正确的权限设置为无济于事,并且目前还没有发现有人遇到这样的问题,例如服务器试图运行另一个脚本。我的问题是如何停止发生此类错误?
编辑:添加了JavaScript代码,主服务器文件夹中的ls-l图像和js脚本文件夹。
Express HTML错误日志(已修改):
<h1>spawn EACCES</h1>
<h2></h2>
<pre>Error: spawn EACCES
at exports._errnoException (util.js:870:11)
at ChildProcess.spawn (internal/child_process.js:298:11)
at exports.spawn (child_process.js:362:9)
at new PythonShell (/home/user_name/simple_server/node_modules/python-shell/index.js:59:25)
at run_py_script (/home/user_name/simple_server/routes/rain_track.js:11:19)
at /home/user_name/simple_server/routes/rain_track.js:43:5
at Layer.handle [as handle_request] (/home/user_name/simple_server/node_modules/express/lib/router/layer.js:95:5)
at next (/home/user_name/simple_server/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/user_name/simple_server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/user_name/simple_server/node_modules/express/lib/router/layer.js:95:5)</pre>
JavaScript代码:
var express = require('express');
var router = express.Router();
var PythonShell = require('python-shell');
var options = {
mode: 'json',
pythonPath: '/home/user_name/simple_server'
};
function run_py_script(data, res, callback){
var pyshell = new PythonShell('dummy.py', options);
console.log(data.latitude + ", " + data.longitude + "\n");
pyshell.send(""+data.latitude + ", "+ data.longitude+"\n"); // change to data
pyshell.on('message', function(message){
console.log(message);
res.json(message);
});
pyshell.end(function(err){
if (err) {
console.log("Python script error has occured.");
console.log(err);
}
//return err ? callback(null) : callback(null, ret_val);
});
}
/* GET rain_track data. */
router.post('/', function(req, res, next) {
console.log("Location got: "+req.body.coords.latitude + ", " + req.body.coords.longitude);
var location_data = {
"latitude" : req.body.coords.latitude,
"longitude" : req.body.coords.longitude
};
run_py_script(location_data, res, function(err, rain_data) {
if (err){ console.log("error in python script" ); return res.json(null); }
})
});
module.exports = router;
ls -l
:最佳答案
经过详细测试,我无法找到解决此问题的方法,并且问题确实已经消失。
但是,我成功找到了解决方法,发现EACCES的问题出在python路径(在options对象中)。删除它并在服务器终端的运行位置(例如/bin
目录)中运行python脚本仍然可以正常工作。