Skip to content

Instantly share code, notes, and snippets.

@xiantail
Last active December 28, 2015 09:17
Show Gist options
  • Save xiantail/f484d24226af3583c7bf to your computer and use it in GitHub Desktop.
Save xiantail/f484d24226af3583c7bf to your computer and use it in GitHub Desktop.
Introducing Python / Chap.11 Exercise 11.2
import zmq
from datetime import datetime
from time import sleep
host = '127.0.0.1'
port = 6789
context = zmq.Context()
client = context.socket(zmq.REQ)
client.connect("tcp://%s:%s" % (host, port))
print("Client started at %s" % datetime.now())
while True:
sleep(3)
request = b'time'
client.send(request)
reply = client.recv()
print("Client received %s" % reply)
import zmq
from datetime import datetime
host = '127.0.0.1'
port = 6789
context = zmq.Context()
server = context.socket(zmq.REP)
server.bind("tcp://%s:%s" % (host, port))
print('Server started at %s' % datetime.now())
while True:
# Wait for next request from client
message = server.recv()
if message == b'time':
now = datetime.now()
reply = str(now)
server.send(bytes(reply, 'utf-8'))
print('Sever sent', reply)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment