Simple TCP to ZeroMQ Message Forwarder with Eventlet

I spent part of the day playing with eventlet today. Eventlet recently added zeromq support. Here’s a very simple message forwarder that sends line terminated messages sent over TCP to a zeromq push socket, using the zeromq hub:

import eventlet
from eventlet.green import socket, zmq


def read_socket(writer, reader):
    message = reader.readline()
    while message:
        writer.send(message)
        message = reader.readline()

def listen_socket(address, port):
    eventlet.hubs.use_hub("zeromq")    
    server = eventlet.listen((address, port))
    while True:
        new_connection, address = server.accept()
        new_zmq_context = zmq.Context()
        new_zmq_push_socket = new_zmq_context.socket(zmq.PUSH)
        new_zmq_push_socket.connect("tcp://127.0.0.1:5558")
        eventlet.spawn_n(read_socket, new_zmq_push_socket, new_connection.makefile('r'))

if __name__ == '__main__':
    listen_socket('127.0.0.1', 7000)

About taotetek

Sometimes stereotypical but never ironic. You can't stop the signal, Mal. All my opinions are my own, unless I stole them from you.
This entry was posted in Python, zeromq and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s