arundhaj

regression towards the datascience

Getting Started with ZeroMQ in Python

 

A simple getting started sample for ZeroMQ in Python. We will implement a Echo Server and Echo Client.

echo_server.py

import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://127.0.0.1:5000")

while True:
    msg = socket.recv()
    print "Got", msg
    socket.send(msg)

echo_client.py

import zmq

context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://127.0.0.1:5000")

while True:
    msg = raw_input("Enter msg:")
    socket.send(msg)
    msg_in = socket.recv()
    print "GOT", msg_in

Comments