【发布时间】:2017-03-18 15:17:46
【问题描述】:
我有一个 python 程序,我想使用 Node JS 在 HTML 页面上打印该程序的输出。正在使用“child_process”调用 python 脚本。我想在 HTML 页面上打印 't' 的值。有什么办法吗?
脚本.py
import time
t=0
def main():
while 1:
t =t+1
print t
time.sleep(2)
# Start process
if __name__ == '__main__':
main()
这里我想每 2 秒打印一次 't' 的值。
app.js
var sys =require('sys');
var myPythonScript ="script.py";
var path =resolve("C:\\Python27\\python.exe");
var pythonExecutable = path;
const spawn = require('child_process').spawn;
const scriptExecution = spawn(pythonExecutable, [myPythonScript]);
scriptExecution.stdout.on('data', (data) => {
console.log(data);
});
});
当点击 HTML 页面上的按钮时调用此 JS 函数。
【问题讨论】:
标签: javascript python html node.js