Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created April 11, 2012 18:07
Show Gist options
  • Save lebedov/2361034 to your computer and use it in GitHub Desktop.
Save lebedov/2361034 to your computer and use it in GitHub Desktop.
Pass interactively entered data to a server process using pyzmq
#!/usr/bin/env python
"""
Pass interactively entered data to a server process using pyzmq.
"""
import zmq
import multiprocessing as mp
def server_func():
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
while True:
message = socket.recv()
print "Server: ", message
socket.send("server reply")
if message == 'quit':
break
def client_func():
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
while True:
data = raw_input('? ')
socket.send(data)
if data == 'quit':
break
message = socket.recv()
print "Client: ", message
server = mp.Process(target=server_func)
server.start()
client_func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment