我在Raspberry Pi上的node.js中编写了一个服务器。 我想从它运行一个python脚本,但它不执行该行。在以前的实例中,我已经运行文件但是来到socket.io的这个实例我无法运行该文件!虽然控制台显示从客户端传递给它的准确输出但它无法读取文件。插座负责移动。
/*jshint esversion: 6 */
var express = require('express');
// library for executing system calls
const spawn = require('child_process').spawn;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server);
var colour = require('colors');
var ip = require('ip');
var PortNumber = 3000;
var xPos = 0, yPos = 0;
server.listen(PortNumber);
app.use(express.static('public'));
io.sockets.on('connection', function (socket) {
socket.emit('ip', {value: 'http://' + ip.address() + ':8081'});
// otp generation socket
socket.on('userEmail', function (data) {
var userEmail = data.value;
// system call to generate OTP
const OTPgeneration = spawn('python', [__dirname + '/python/OTPgeneration.py', userEmail]);
OTPgeneration.stdout.on('data', (data) => {
console.log(`${data}`);
});
});
// otp submit button socket
socket.on('userOtp', function (data) {
var userOtp = data.value;
console.log(userOtp);
// system call to generate OTP
const otpValidate = spawn('python', [__dirname + '/python/OTPvalidation.py', userOtp]);
otpValidate.stdout.on('data', (data) => {
console.log(`${data}`);
// Get last output from python file
var lastChar = `${data}`.slice(-2);
if (lastChar === '0\n') {
console.log('Wrong OTP');
}
else io.sockets.emit('otpConformation', {value: 'confirm'}); //sends the confirmation to all connected clients
});
});
// x y values socket
socket.on('servoPosition', function (data) {
servoPosition = data.value;
// servoPosition[0] is x and servoPosition[1] is y
console.log('Reveived X: ' + servoPosition[0] + ' Y: ' + servoPosition[1]);
// system call for i2c comminication
const i2cData = spawn('python', [__dirname + '/python/sendI2C.py', servoPosition[0], servoPosition[1]]);
i2cData.stdout.on('data', (data) => {
console.log(`${data}`);
});
});
// movement socket
socket.on('movement', function (data) {
var m = data.value;
console.log("Movement :" + m);
const submitMove = spawn('python', [__dirname + '/python/move.py', m]);
submitMove.stdout.on('data', (data) => {
console.log(`${data}`);
});
});
});
console.log('Server Running @'.green + ip.address().green + ":3000".green);
function readTextFile(file) {
var rawFile = new XMLHttpRequest();
//var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if(rawFile.readyState === 4) {
if(rawFile.status === 200 || rawFile.status === 0) {
allText = rawFile.responseText;
//alert(allText);
}
}
};
rawFile.send(null);
}
答案 0 :(得分:0)
虽然使用Node的本机“child_process”模块是性能最高的选项,但让它工作起来可能会很乏味。快速解决方案是使用node-cmd
模块。
运行npm i node-cmd --save
进行安装。
通过服务器文件顶部的var cmd = require('node-cmd');
导入。然后,
var pyProcess = cmd.get('python YOURPYTHONSCRIPT.py',
function(data, err, stderr) {
if (!err) {
console.log("data from python script " + data)
} else {
console.log("python script cmd error: " + err)
}
}
);
根据需要进行编辑。 node-cmd
有两种方法可以调用它,get
和run
。 run
只运行提供的CLI命令,而get
提供回调的使用,允许更好的错误处理,并使stdin
,stdout
,stderr
之间的工作更进一步更容易。