2017-08-13 111 views
0

我是间歇性的python,我使用套接字模块接收市场数据并通过API从Broker应用程序订购数据。我仍然困惑如何编码接收多个数据长度和标题,根据标题信息,我会处理数据。从Socket流服务器接收多个数据长度 - PYTHON3

如何接收多个数据长度并决定用正确的结构格式函数解压缩?

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))) 
+0

我想从你的extream中知道以下知识注意:方法'StartTTAPI'调用并在线程中运行。 如何接收多个数据长度并决定使用正确的结构格式函数解压缩? 频繁地得到Exception'struct.error'as-unpack需要一个长度为xxx的字节对象(对于所有的解包方法) 几分钟后它不会从服务器接收任何数据,但服务器正在流数据 我提醒它没有收到所有数据,而服务器正在收集数据,并且从不抛出任何异常或错误。 –

回答

1

当你调用self.sock.recv(1024)从0(套接字已关闭),以1024个字节的数据,你可以在任何地方收到。您可能没有收到足够的数据来解开一个完整的消息,可能需要继续接收和缓冲数据,直到您有一个完整的解压缩消息,并且可能还会收到下一条消息的一部分。 TCP是一种没有消息边界指示符的流协议。使用TCP的协议必须定义完整消息的外观。

没有更多的协议细节,我只能组成一个协议并给出一个例子。这完全没有经过测试,但应该给你一个想法。

协议:4个字节长(大端)后跟数据字节。

def get_message(self): 
    # read the 4-byte length 
    data = self.get_bytes(4) 
    if not data: 
     return None # indicates socket closed 
    if len(data) < 4: 
     raise IncompleteMessageError 

    # process the length and fetch the data 
    length = struct.unpack('>L',data)  
    data = self.get_bytes(length) 
    if len(data) < length: 
     raise IncompleteMessageError 
    return data 

def get_bytes(self,count): 
    # buffer data until the requested size is present 
    while len(self.buffer) < count: 
     new_data = self.sock.recv(1024) 
     if not new_data: # socket closed 
      remaining,self.buffer = self.buffer,b'' 
      return remaining # return whatever is left. 
     self.buffer += new_data 

    # split off the requested data from the front of the buffer 
    data,self.buffer = self.buffer[:count],self.buffer[count:] 
    return data 
+0

我想从你的extream知识中了解以下内容注意:方法“StartTTAPI”调用并在线程中运行。 如何接收多个数据长度并决定使用正确的结构格式函数解压缩? 频繁地得到Exception'struct.error'as-unpack需要一个长度为xxx的字节对象(对于所有的解包方法) 几分钟后它不会从服务器接收任何数据,但服务器正在流数据 我提醒它没有收到所有数据,而服务器正在收集数据,并且从不抛出任何异常或错误。 –

+0

@manjunathan请提出一个新问题。你已经编辑了你现有的问题,这使得当前的答案变得没有意义。我冒昧地回滚你的编辑。相反,如果您为当前问题添加更多详细信息(例如协议详细信息),则可以编辑我的答案以针对您的问题,但不要完全改变问题。 –

+0

好吧,会问在新的问题,我很抱歉,即时新到stackoverflow –

相关问题