Code in Python, communicate in Node.js and Socket.IO, present in HTML

微信扫一扫,分享到朋友圈

Code in Python, communicate in Node.js and Socket.IO, present in HTML

You have a python script diagnosis.py that generates realtime event-based data. Using Node.js , you can launch it as a child process and capture its output and then using Socket.IO emit that to the client and present it using HTML.

Server

var util  = require('util'),
    spawn = require('child_process').spawn,
    ls    = spawn('python', ['diagnosis.py']);

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(80);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
    ls.stdout.on('data', function (gdata) {
      socket.emit('news', gdata.toString());
    });
});

Client

    
        
        
          var d = "";
          var socket = io.connect('http://localhost');
          socket.on('news', function (data) {
            d += data;
            document.getElementById('data').innerHTML = d;
            console.log(data);
          });
        
    
    
        

Question

This is great and all, but what if you’re looking for the same HTML- Node.js communicative power that Socket.IO provides but instead between Node.js and Python? How would you do that? There’s no web server there, so Socket.IO does not make a lot of sense and communicating over bare TCP does not provide the same power/elegance. How do I achieve full duplex communication between Node.js and Python?

UpdateI answered my own question, but I’m open to an alternative approach. RPC doesn’t quite do what I want though.

Problem courtesy of: melhosseiny

Solution

I used the library inspired by this question to turn diagnosis.py into a Socket.IO client. This way I can emit the realtime data to the Node.js Socket.IO server:

socketIO.emit('gaze', ...)

And then have it do a socket.broadcast.emit to emit the data to all the Socket.IO clients (browser and diagnosis.py ).

RPC is probably the more standard approach for cross-language development but I find it’s a bit of an overkill to do that when the goal is to exchange data. It also does not support evented IO out of the box.

Update on Jan 2013Since socket.broadcast.emit generates a lot of unnecessary traffic, I tried to find a better way of doing this. The solution I came up with is to use namespaces which is supported by the basic python Socket.IO client library I mentioned.

Python

self.mainSocket = SocketIO('localhost', 80)
self.gazeSocket = self.mainSocket.connect('/gaze')
self.gazeSocket.emit('gaze', ...)

To connect to the gaze namespace.

Node.js

var gaze = io.of('/gaze').on('connection', function (socket) {
    socket.on('gaze', function (gdata) {
        gaze.emit('gaze', gdata.toString());
    });
});

This emits the data received only to clients connected to the gaze namespace.

Browser

var socket = io.connect('http://localhost/gaze');
socket.on('gaze', function (data) {
    console.log(data);
});

Solution courtesy of: melhosseiny

Discussion

If you’re just looking for something that gives you a simple protocol on top of sockets, so you don’t have to deal with buffering and delimiting messages and all that stuff, the two obvious choices (depending on what kind of data you’re trying to send) are netstrings and JSON-RPC. There are multiple choices of libraries for both, in both languages, but you can end up with code as simple as this:

class MyService(Service):
    # … code to set up the newsdb
    def getnews(self, category, item):
        return self.newsdb.get(category, item)
myService = MyService(6000)

myService = Service(6000)
// … code to set up the newsdb
myService.getnews = function(category, item, callback) {
    callback(this.newsdb.get(category, item);
}

Discussion courtesy of: abarnert

Apache Thrift is a pretty awesome way to write RPC code between all of the major languages. You write a generic Thrift spec declaring the types and services, and then the code generator creates bindings for your desired languages. You can have apis for calling methods between your node and python code bases.

On a more generic low level approach, ZeroMQ is a message queue library that also has support for all of the major languages. With this, you can design your own solution for how you want to communicate (not just purely RPC). It has patterns for request/reply, push/pull, pub/sub, and pair. It gives you enough tools to put together a scalable system of any type.

I have used both and they are great solutions.

Just as a very rough example, the Thrift spec may be something like this. Lets say you want to communicate events from python to node.js , have it processed and get back some response:

myspec.thrift

struct Event {
    1: string message; 
}

service RpcService {
    string eventOccurred(1:Event e)
}

This defines a data structure called Event with a single string member to hold the message. Then a service called RpcService define one function called eventOccured which expects an Event as an argument, and will return a string.

When you generate this code for python and node.js , you can then use the client side code for python and the server side code for node.js

python

from myrpc import RpcService, ttypes

# create a connection somewhere in your code
CONN = connect_to_node(port=9000)

def someoneClickedSomething():
    event = ttypes.Event("Someone Clicked!")
    resp = CONN.eventOccurred(event)
    print "Got reply:", resp

node.js

// I don't know node.js, so this is just pseudo-code

var thrift = require('thrift');
var myrpc = require('myrpc.js');

var server = thrift.createServer(myrpc.RpcService, {
  eventOccurred: function(event) {
    console.log("event occured:", event.message);
    success("Event Processed.");
  },
});
server.listen(9000);

Discussion courtesy of: jdi

You can look at some messaging systems like 0mq http://www.zeromq.org/

Discussion courtesy of: Vallières

This recipe can be found in it’s original form on Stack Over Flow .

用小程序切入图书管理市场,「今日图书」在做的是一门好生意吗?

上一篇

36氪首发 | 集合机器人触觉、AI、VR、AR等众多技术辅助医疗,「IHS」获得500万种子轮融资

下一篇

你也可能喜欢

Code in Python, communicate in Node.js and Socket.IO, present in HTML

长按储存图像,分享给朋友