Guest User

Untitled

a guest
Dec 30th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #
  2. # serv.py
  3. #
  4. #!/usr/bin/python3
  5.  
  6. import time
  7. import zmq
  8.  
  9. ctx = zmq.Context.instance()
  10. ctx.set(zmq.BLOCKY, False)
  11. ctx.setsockopt(zmq.LINGER, 0)
  12.  
  13. sock = ctx.socket(zmq.ROUTER)
  14. sock.setsockopt(zmq.LINGER, 0)
  15. sock.setsockopt(zmq.ROUTER_MANDATORY, 1)
  16. sock.setsockopt(zmq.ROUTER_HANDOVER, 1)
  17. sock.bind('tcp://0.0.0.0:9999')
  18. print("bound to 9999")
  19.  
  20. while True:
  21.     time.sleep(0.6)
  22.     wid, msg = sock.recv_multipart()
  23.     print("raw routing id: {}".format(wid))
  24.     print("decoded routing id: {}".format(wid.decode()))
  25.     print("{} ... (20/{})".format(msg[:20], len(msg)))
  26.  
  27.  
  28. #
  29. # client.py
  30. #
  31. #!/usr/bin/python3
  32.  
  33. import zmq
  34. import time
  35. import sys
  36.  
  37. if len(sys.argv) == 2:
  38.     tag = sys.argv[1]
  39. else:
  40.     tag = 'notag'
  41.  
  42. ctx = zmq.Context.instance()
  43. ctx.set(zmq.BLOCKY, False)
  44. ctx.setsockopt(zmq.LINGER, 0)
  45.  
  46. sock = ctx.socket(zmq.DEALER)
  47. sock.setsockopt(zmq.LINGER, 0)
  48. sock.setsockopt(zmq.ROUTING_ID, ('myroutingid' + tag).encode())
  49.  
  50. sock.connect('tcp://localhost:9999')
  51. print("sending first")
  52. sock.send(('first {}'.format(tag) * 50).encode())
  53. time.sleep(1)
  54. print("sending second")
  55. sock.send(('second {}'.format(tag) * 50).encode())
  56. sock.close()
  57. ctx.term()
Advertisement
Add Comment
Please, Sign In to add comment