You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zmq_sub3.4.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 The Starwels developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. """
  6. ZMQ example using python3's asyncio
  7. Starwels should be started with the command line arguments:
  8. starwelsd -ai -daemon \
  9. -zmqpubhashblock=tcp://127.0.0.1:28352 \
  10. -zmqpubrawtx=tcp://127.0.0.1:28352 \
  11. -zmqpubhashtx=tcp://127.0.0.1:28352 \
  12. -zmqpubhashblock=tcp://127.0.0.1:28352
  13. We use the asyncio library here. `self.handle()` installs itself as a
  14. future at the end of the function. Since it never returns with the event
  15. loop having an empty stack of futures, this creates an infinite loop. An
  16. alternative is to wrap the contents of `handle` inside `while True`.
  17. The `@asyncio.coroutine` decorator and the `yield from` syntax found here
  18. was introduced in python 3.4 and has been deprecated in favor of the `async`
  19. and `await` keywords respectively.
  20. A blocking example using python 2.7 can be obtained from the git history:
  21. https://github.com/starwels/starwels/blob/37a7fe9e440b83e2364d5498931253937abe9294/contrib/zmq/zmq_sub.py
  22. """
  23. import binascii
  24. import asyncio
  25. import zmq
  26. import zmq.asyncio
  27. import signal
  28. import struct
  29. import sys
  30. if not (sys.version_info.major >= 3 and sys.version_info.minor >= 4):
  31. print("This example only works with Python 3.4 and greater")
  32. exit(1)
  33. port = 28352
  34. class ZMQHandler():
  35. def __init__(self):
  36. self.loop = zmq.asyncio.install()
  37. self.zmqContext = zmq.asyncio.Context()
  38. self.zmqSubSocket = self.zmqContext.socket(zmq.SUB)
  39. self.zmqSubSocket.setsockopt_string(zmq.SUBSCRIBE, "hashblock")
  40. self.zmqSubSocket.setsockopt_string(zmq.SUBSCRIBE, "hashtx")
  41. self.zmqSubSocket.setsockopt_string(zmq.SUBSCRIBE, "rawblock")
  42. self.zmqSubSocket.setsockopt_string(zmq.SUBSCRIBE, "rawtx")
  43. self.zmqSubSocket.connect("tcp://127.0.0.1:%i" % port)
  44. @asyncio.coroutine
  45. def handle(self) :
  46. msg = yield from self.zmqSubSocket.recv_multipart()
  47. topic = msg[0]
  48. body = msg[1]
  49. sequence = "Unknown"
  50. if len(msg[-1]) == 4:
  51. msgSequence = struct.unpack('<I', msg[-1])[-1]
  52. sequence = str(msgSequence)
  53. if topic == b"hashblock":
  54. print('- HASH BLOCK ('+sequence+') -')
  55. print(binascii.hexlify(body))
  56. elif topic == b"hashtx":
  57. print('- HASH TX ('+sequence+') -')
  58. print(binascii.hexlify(body))
  59. elif topic == b"rawblock":
  60. print('- RAW BLOCK HEADER ('+sequence+') -')
  61. print(binascii.hexlify(body[:80]))
  62. elif topic == b"rawtx":
  63. print('- RAW TX ('+sequence+') -')
  64. print(binascii.hexlify(body))
  65. # schedule ourselves to receive the next message
  66. asyncio.ensure_future(self.handle())
  67. def start(self):
  68. self.loop.add_signal_handler(signal.SIGINT, self.stop)
  69. self.loop.create_task(self.handle())
  70. self.loop.run_forever()
  71. def stop(self):
  72. self.loop.stop()
  73. self.zmqContext.destroy()
  74. daemon = ZMQHandler()
  75. daemon.start()