【问题标题】:Using websocket node to stream commands to python shell使用 websocket 节点将命令流式传输到 python shell
【发布时间】:2013-11-02 21:33:27
【问题描述】:

我试图构建一个通过终端命令行访问 python shell 的 Web 应用程序,但我在处理流时遇到了一些困难。

我有一个 node.js/websocket 服务器,它将传入命令发送到 python 进程的标准输入(下面的代码),这似乎工作正常,但由于某种原因,每个传入命令都会发送多个响应消息。例如,如果发送命令'hello world',则会将多个'hello world's 发送回客户端(连同多个>>> 通过stderr)。我很困惑......我已经确保只有一个命令来自客户端,那么为什么我会收到很多响应?

var python = spawn('python', ['-i']);
s.on('run', function(input) {
  console.log('Running command: ' + input.command);
  python.stdin.write(input.command + '\n');
  python.stdout.on('data', function(data) {                                                                                                                  
    var output = '' + data;
    s.emit('response', {
      output: output
    }); 
    console.log('python stdout: ' + output);
  }); 
  python.stderr.on('data', function(data) {
    var output = '' + data;
    s.emit('response', {
      error: output
    }); 
    console.log('python stderr: ' + output);
  }); 
}); 

【问题讨论】:

    标签: node.js stream websocket socket.io node.js-stream


    【解决方案1】:

    因为每次用户(客户端)发出run 事件时,您都会添加一个Event Listener。 在前。 10 run events ,它将有 10 个事件侦听器正在侦听 stdout 和 stderr 的事件。

    你应该把stdout.on(..)stderr.on(..)移到.on('run',..)之外

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 2020-04-10
      • 2020-04-06
      • 2014-03-19
      • 2019-05-25
      相关资源
      最近更新 更多