I'm intermittent to python, I'm using socket module to receive market data and Order data from Broker Application via API. I'm still confusing how to code for receiving the multiple data length and header, based on the header info, I will treat the data.
How to receive multiple Data length and decide to unpack with correct struct format function?
while True:
try:
"""
continously receive data from Server API (stock Market data streaming)
"""
brecvd = self.sock.recv(1024)
self.brecvdsize = len(brecvd)
# Unpack the header for correct struct formate to unpack
unpkr = self.struct.Struct('<lh')
recvd =self.struct.Struct.unpack_from(unpkr, brecvd)
Marketdepth = recvd[0] == 284 and recvd[1] == 26
Indices = recvd[0] == 228 and recvd[1] == 27
Feed = recvd[0] == 384 and recvd[1] == 22
BidOffer = recvd[0] == 145 and recvd[1] == 28
Msg = recvd[0] == 360 and recvd[1] == 99
#Msg to be checked for 260 or 360
if Marketdepth:
self.Marketdepthresponse(brecvd)
Marketdepth = False
elif Indices:
self.Indicesresponse(brecvd)
Indices = False
elif Feed:
self.feedresponse(brecvd)
Feed = False
elif BidOffer:
self.Bidoffer(brecvd)
BidOffer = False
elif Msg:
self.GeneralMsg(brecvd)
Msg = False
else:
Marketdepth = False
Indices = False
Feed = False
BidOffer = False
Msg = False
pass
except Exception as e:
self.errorcount += 1
print('***Run Loop Receive Issue: {0}'.format(str(e)))