将消息从python-shell发送到Node Express App中的浏览器 [英] Emitting messages from python-shell to a browser in a Node Express App

查看:105
本文介绍了将消息从python-shell发送到Node Express App中的浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Express应用,我希望能够通过路由触发python脚本并将日志发送到浏览器.

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脚本,并使用python-shell模块将日志输出到节点控制台.如何将该日志实时推送到浏览器.

I have created a route which triggers the python scripts correctly and outputs the log into the node console using python-shell module. How can I push this log to the browser in real time.

我在index.html页面上有一个按钮,该按钮触发对以下/python路由的Ajax发布请求.我尝试使用socket.io实现此功能,但没有设法解决. socket.io是前进的方向吗,请向我指出正确的方向,或者有人可以推荐替代方法吗?

I have a button on an index.html page which triggers an Ajax post request to a /python route below. I have tried to implement this with socket.io but haven't managed to get my head around it. Is socket.io the way forward, please point me in the right direction or can someone recommend an alternative?

我的文件布局为:

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

app/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)
      });

}

预先感谢

推荐答案

socket.io是一个很好的解决方案.这将负责将消息从服​​务器传递到客户端.您只需将消息发布到服务器端,然后在客户端对它进行回调

socket.io is a good solution. 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>

这篇关于将消息从python-shell发送到Node Express App中的浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆