[英]How to include python script inside a NodeJS file?
Good day everyone, 今天是个好日子,
Through recommendation of this community, I have install shelljs such as npm install shelljs --save
into my app folder. 通过这个社区的建议,我已经将shelljs安装了,例如
npm install shelljs --save
到我的应用程序文件夹中。
Now I am working out on how to implement it in my nodejs file. 现在,我正在研究如何在我的nodejs文件中实现它。
Below is my nodejs file 下面是我的nodejs文件
var express = require('express');
var router = express.Router();
var COMPORT = 5;
var command = 'option1';
Below this i would like to include my python script 在此之下,我想包括我的python脚本
import serial
ser = serial.Serial()
ser.baudrate = 38400 #Suggested rate in Southco documentation, both locks and program MUST be at same rate
// COMPORT is a variable that stores an integer such as 6
ser.port = "COM{}".format(COMPORT)
ser.timeout = 10
ser.open()
#call the serial_connection() function
ser.write(("%s\r\n"%command).encode('ascii'))
Hence, my question is how do you include this python script with the variables defined in nodejs included in the script in the same file as the nodejs. 因此,我的问题是,如何将这个python脚本与nodejs中定义的变量包括在脚本中,该变量包含在与nodejs相同的文件中。
This app that runs on nodejs will be package as an executable desktop app via electron. 在nodejs上运行的此应用将通过电子程序打包为可执行的桌面应用。
I think it is the easiest way to run python script as child process. 我认为这是将python脚本作为子进程运行的最简单方法。
const childProcess = require('child_process'),
cmd = './script.py --opt=' + anyValueToPass;
childProcess.exec(cmd, function(err, stdout, stderr) {
// command output is in stdout
});
Read more about child process at: Execute a command line binary with Node.js 在以下位置了解有关子进程的更多信息: 使用Node.js执行命令行二进制文件
In this way, you ned to care for command injection vulnerability. 这样,您就可以照顾命令注入漏洞。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.