-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrep.py
35 lines (27 loc) · 942 Bytes
/
rep.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 time
import zmq
import msgpack
LOG = logging.getLogger(__name__)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--listen", help="listen", required=True,
action="store", 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()
sock = context.socket(zmq.REP)
sock.bind(args.listen)
send_msg = {"key1": "val1", "key2": 2}
send_bin = msgpack.packb(send_msg)
while True:
try:
recv_msg = msgpack.unpackb(sock.recv(), encoding='utf-8')
LOG.info("Received as REQ: %s", recv_msg)
sock.send(send_bin)
LOG.info("Sent as REP: %s", send_msg)
except KeyboardInterrupt:
break