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 import socket, zmq

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

def listen_socket(address, port):
    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)
        eventlet.spawn_n(read_socket, new_zmq_push_socket, new_connection.makefile('r'))

if __name__ == '__main__':
    listen_socket('', 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: Logo

You are commenting using your 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