2016-11-22 52 views
0

美好的一天大家,如何在一個NodeJS文件中包含python腳本?

通過這個社區的推薦,我已經安裝shelljs,如npm install shelljs --save到我的應用程序文件夾。

現在我正在研究如何在我的nodejs文件中實現它。

下面是我的NodeJS文件

var express = require('express'); 
var router = express.Router(); 
var COMPORT = 5; 
var command = 'option1'; 

下面這個我想包括我的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')) 

因此,我的問題是你如何包括與中定義的變量,這個python腳本nodejs包含在與nodejs相同的文件中的腳本中。

在nodejs上運行的此應用程序將通過電子封裝爲可執行的桌面應用程序。

回答

0

我認爲這是運行Python腳本作爲子進程的最簡單方法。

const childProcess = require('child_process'), 
     cmd = './script.py --opt=' + anyValueToPass; 

childProcess.exec(cmd, function(err, stdout, stderr) { 
    // command output is in stdout 
}); 

在閱讀更多關於子進程:Execute a command line binary with Node.js

這樣,你定義照顧命令注入漏洞。

相關問題