【发布时间】:2017-08-10 17:32:03
【问题描述】:
我在代码 sn-ps 之后指定了下面的问题。
app.js
app.route('/conn').post(function (req,res,next) {
var sys =require('util');
var myPythonScript ="script1.py";
var path =resolve("C:\\Python27\\python.exe");
var pythonExecutable = path;
var uint8arrayToString = function(data){
return String.fromCharCode.apply(null, data);
};
const spawn = require('child_process').spawn;
const scriptExecution = spawn(pythonExecutable, [myPythonScript]);
// Handle normal output
var chunk =' ';
scriptExecution.stdout.on('data', function(data) {
chunk +=data;
});
scriptExecution.stdout.on('data', function(data) {
console.log(chunk);
});
// Handle error output
scriptExecution.stderr.on('data', (data) => {
// As said before, convert the Uint8Array to a readable string.
console.log(uint8arrayToString(data));
});
scriptExecution.on('exit', (code) => {
//console.log("Process quit with code : " + code);
});
res.write('<h2>chunk</h2>');
// End data write
//scriptExecution.stdin.end();
});
上传.html
<html>
<form action ="/file" method ="post">
MAC Address:<br>
<input type="text" name="macadd" id="macadd"><br>
Percentage:<br>
<input type="text" name="percent" id="percent"><br>
<input type="submit" value='Submit' id="upload">
<br>
<input type="reset" value="Reset">
</form>
<input type=button onClick="location.href='/home'" value='Home'><br>
<form action ="/conn" method= "post">
<input type="submit" value='Show Users'><br>
</form>
</html>
script1.py
import sys,time
def main():
t=0
while t!=5:
t =t+1
print t
# Start process
if __name__ == '__main__':
main()
t 的值存储在“块”变量中,通过 console.log() 我可以在控制台上看到 t 的值。我想在upload.html 页面上显示这些值。我该怎么做呢?我尝试使用 res.render 和 res.write 函数,但没有显示任何内容。有什么办法吗?
【问题讨论】:
-
使用承诺。类似gist.github.com/Stuk/6226938 的东西用于生成子进程并在
then()中调用res.write()
标签: javascript html node.js