-
Notifications
You must be signed in to change notification settings - Fork 7
/
app_client.py
executable file
·71 lines (45 loc) · 1.58 KB
/
app_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python3
import sys
import zmq
from datetime import datetime, timedelta
from zmq.eventloop.ioloop import IOLoop, PeriodicCallback
from zmq.eventloop.zmqstream import ZMQStream
from zmq_msg_helo import *
from key_monkey import *
class AppClient(object):
# where to connect to - point to another IP or hostname:5556 if running server on another machine
endpoint = "tcp://127.0.0.1:5556"
# crypto = True means 'use CurveZMQ'. False means don't. Must match server.
crypto = True
def __init__(self):
self.ctx = zmq.Context()
self.loop = IOLoop.instance()
self.client = self.ctx.socket(zmq.DEALER)
if self.crypto:
self.keymonkey = KeyMonkey("client")
self.client = self.keymonkey.setupClient(self.client, self.endpoint, "server")
self.client.connect(self.endpoint)
print("Connecting to", self.endpoint)
self.client = ZMQStream(self.client)
self.client.on_recv(self.on_recv)
self.periodic = PeriodicCallback(self.periodictask, 1000)
self.last_recv = None
def periodictask(self):
if not self.last_recv or self.last_recv + timedelta(seconds=5) < datetime.utcnow():
print("Hmmm... haven't heard from the server in 5 seconds... Server unresponsive.")
print("Sending HELLO to server")
msg = HelloMessage()
msg.send(self.client)
def start(self):
self.periodic.start()
try:
self.loop.start()
except KeyboardInterrupt:
pass
def on_recv(self, msg):
self.last_recv = datetime.utcnow()
print("Received a message of type %s from server!" % msg[0])
if __name__ == '__main__':
my_client = AppClient()
my_client.start()
# vim: ts=4 sw=4 noet