Last active
December 28, 2015 09:17
-
-
Save xiantail/f484d24226af3583c7bf to your computer and use it in GitHub Desktop.
Introducing Python / Chap.11 Exercise 11.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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