Skip to content

Instantly share code, notes, and snippets.

@jhodges10
Created September 18, 2018 20:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhodges10/91a376e8c534a9dcd6b13a99d343f8f8 to your computer and use it in GitHub Desktop.
Save jhodges10/91a376e8c534a9dcd6b13a99d343f8f8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import binascii
import zmq
import struct
import csv
import time
port = 5556
def write_csv(tstamp, type, value, sequence):
with open("../../../messages.csv", 'wb') as mlog_file:
msg_logger = csv.writer(mlog_file, delimter=',')
msg_logger.writerow([tstamp, type, value, sequence])
return True
def zmq_tx_consumer():
zmqContext = zmq.Context()
zmqSubSocket = zmqContext.socket(zmq.SUB)
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"tx")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"sn")
zmqSubSocket.connect("tcp://zmq.devnet.iota.org:%i" % port)
try:
while True:
msg = zmqSubSocket.recv_multipart()
topic = str(msg[0].decode("utf-8"))
body = msg[1]
sequence = "Unknown";
if len(msg[-1]) == 4:
msgSequence = struct.unpack('<I', msg[-1])[-1]
sequence = str(msgSequence)
if topic == "tx":
print('- tx ('+sequence+') -')
tx = binascii.hexlify(body).decode("utf-8")
print(tx)
elif topic == "sn":
print('- sn ('+sequence+') -')
sn = binascii.hexlify(body).decode("utf-8")
print(sn)
else:
pass
except KeyboardInterrupt:
zmqContext.destroy()
if __name__ == '__main__':
print("Starting ZMQ Listener...")
zmq_tx_consumer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment