简体   繁体   English

将消息从python-shell发送到Node Express App中的浏览器

[英]Emitting messages from python-shell to a browser in a Node Express App

I have an express app and I would like to be able to trigger python scripts via routes and emit the log into the browser. 我有一个快速应用程序,我希望能够通过路由触发python脚本并将日志发送到浏览器。

I have created a route which triggers the python scripts correctly and outputs the log into the node console using python-shell module. 我创建了一个正确触发python脚本的路由,并使用python-shell模块将日志输出到节点控制台。 How can I push this log to the browser in real time. 如何实时将此日志推送到浏览器。

I have a button on an index.html page which triggers an Ajax post request to a /python route below. 我在index.html页面上有一个按钮,它触发了一个Ajax发布请求到下面的/ python路由。 I have tried to implement this with socket.io but haven't managed to get my head around it. 我试图用socket.io来实现它,但还没有设法解决它。 Is socket.io the way forward, please point me in the right direction or can someone recommend an alternative? socket.io是前进的方向,请指出我正确的方向或有人推荐替代方案?

My file layout is: 我的文件布局是:

server.js server.js

var express      = require('express');
var path         = require('path');
var app          = express();
var port         = process.env.PORT || 3000;
var http         = require('http');
var server       = http.createServer(app);
var io           = require('socket.io').listen(server);

/* A bit inbetween */

server.listen(port)

app/routes.js 应用程序/ routes.js

var PythonShell = require('python-shell');
var config      = require('../config/config');

module.exports = function(app) {
      app.get('/', function(req, res) {
        res.render('pages/index.ejs', {
            pageTitle: 'Index'
        }); // load the index.ejs file
      });

      app.post('/test', function(req, res) {

          var options = {
               mode: 'text',
               pythonPath: config.pythonPath,
               pythonOptions: ['-u'],
               scriptPath: config.pythonScriptsDirectory
             };

             var pyshell = new PythonShell('printRange.py', options);

             pyshell.on('message', function (message) {
               console.log(message);
             });

         res.sendStatus(200)
      });

}

Thanks in advance 提前致谢

socket.io is a good solution. socket.io是一个很好的解决方案。 This will take care of passing the messages from the server to the client. 这将负责将消息从服​​务器传递到客户端。 You just post the message to on the server side, and have a callback react to it on the client side 您只需将消息发布到服务器端,并在客户端对其进行回调

On the server side you'll have something like this: 在服务器端,你会有这样的事情:

var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
io.on('connection', function(){ 
  console.log('connected');
});
server.listen(3000);

...

var pyshell = new PythonShell('printRange.py', options);
pyshell.on('message', function (message) {
  if (connected)
     io.emit('logentry',message);
});

On the client side, you'll have something like this: 在客户端,你会有这样的事情:

<script src='/socket.io/socket.io.js'></script>

<script>
  var socket = io();
  socket.on('logentry',function(log_entry){
    add_to_html(log_entry); // You need to implement this function.
  });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM