Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # serv.py
- #
- #!/usr/bin/python3
- import time
- import zmq
- ctx = zmq.Context.instance()
- ctx.set(zmq.BLOCKY, False)
- ctx.setsockopt(zmq.LINGER, 0)
- sock = ctx.socket(zmq.ROUTER)
- sock.setsockopt(zmq.LINGER, 0)
- sock.setsockopt(zmq.ROUTER_MANDATORY, 1)
- sock.setsockopt(zmq.ROUTER_HANDOVER, 1)
- sock.bind('tcp://0.0.0.0:9999')
- print("bound to 9999")
- while True:
- time.sleep(0.6)
- wid, msg = sock.recv_multipart()
- print("raw routing id: {}".format(wid))
- print("decoded routing id: {}".format(wid.decode()))
- print("{} ... (20/{})".format(msg[:20], len(msg)))
- #
- # client.py
- #
- #!/usr/bin/python3
- import zmq
- import time
- import sys
- if len(sys.argv) == 2:
- tag = sys.argv[1]
- else:
- tag = 'notag'
- ctx = zmq.Context.instance()
- ctx.set(zmq.BLOCKY, False)
- ctx.setsockopt(zmq.LINGER, 0)
- sock = ctx.socket(zmq.DEALER)
- sock.setsockopt(zmq.LINGER, 0)
- sock.setsockopt(zmq.ROUTING_ID, ('myroutingid' + tag).encode())
- sock.connect('tcp://localhost:9999')
- print("sending first")
- sock.send(('first {}'.format(tag) * 50).encode())
- time.sleep(1)
- print("sending second")
- sock.send(('second {}'.format(tag) * 50).encode())
- sock.close()
- ctx.term()
Advertisement
Add Comment
Please, Sign In to add comment