-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
47 lines (39 loc) · 1.31 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import zmq
import msgpack
class Client():
def __init__(self, server="tcp://localhost:5555"):
self.context = zmq.Context()
# Connect to server
print("Connecting to \"%s\"..." % server)
self.socket = self.context.socket(zmq.REQ)
self.socket.connect(server)
def get_reply(self):
# Get reply
reply = msgpack.unpackb(self.socket.recv())
return reply
def request(self, request):
# Send request
self.socket.send(msgpack.packb(request))
if __name__ == "__main__":
client = Client()
for iteration in range(1, 6):
request = {
"quit": False,
"buffer.insert": ("Request: %s\n" % iteration, 0),
"buffer.print": "Request: %s" % iteration
}
if iteration == 5:
request["quit"] = True
try:
# Send request
print("Sending request %s..." % iteration)
client.request(request)
except KeyboardInterrupt:
print("\nRecieved keyboard interrupt. Shutting down...")
break
try:
# Recieve reply
print("Recieved reply: %s" % client.get_reply().decode("UTF-8"))
except KeyboardInterrupt:
print("\nRecieved keyboard interrupt. Shutting down...")
break