-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub.py
36 lines (28 loc) · 997 Bytes
/
sub.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
import argparse
import logging
import msgpack
import zmq
LOG = logging.getLogger(__name__)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--connect", help="host to connect for subscribe",
required=True, action="append", type=str, metavar="URL")
args = parser.parse_args()
lognum = getattr(logging, "INFO")
logging.basicConfig(format='{asctime} {levelname} {module} {message}',
style='{', level=lognum)
context = zmq.Context()
poller = zmq.Poller()
for host in args.connect:
sock = context.socket(zmq.SUB)
sock.connect(host)
sock.setsockopt(zmq.SUBSCRIBE, b'')
poller.register(sock, zmq.POLLIN)
while True:
try:
socks = dict(poller.poll())
except KeyboardInterrupt:
break
for sock in socks:
recv_msg = msgpack.unpackb(sock.recv(), encoding='utf-8')
LOG.info("Received: %s", recv_msg)