我得到了一些代码来接收如下所示的数据:(正在使用 SSL)
sockListToCheck = self.getSocketList(self.clientSocketsWTTL)
socksReady, w, e = select.select(sockListToCheck,[],[], d_consts.SELECT_SEC)
if not socksReady: #list empty
print 'not ready'
return #nothing received yet so don't change anything
leftInError = False
#we have something
for sock in socksReady:
dataBuf = ""
c1 = 5
c2 = 5
while True:
try:
dat = sock.recv(d_consts.BUF_SIZE) # read what is there to read
dataBuf += dat
print(dat), len(dat)
if len(dat) < d_consts.BUF_SIZE and c1 == 0:
c1 -= 1
print 'about to break'
break
except ssl.SSLError as e:
#print 'trying to read '
# Ignore the SSL equivalent of EWOULDBLOCK, but re-raise other errors
#print e
if e.errno != ssl.SSL_ERROR_WANT_READ:
print 'serious ssl error'
sock.shutdown(socket.SHUT_RDWR)
sock.close() #close
leftInError = True
else:
if c2 == 0:
break
c2 -= 1
continue
if (leftInError):
leftInError = False
print 'left error'
continue
当有一些明显的延迟时我遇到了问题,所以我添加了这些计数器。我的问题是,如果我不知道消息有多大,我不知道何时停止接收。当接收到的数量小于缓冲区时,我曾经中断过,但似乎有时会发生这种情况,但并非所有数据都已到达。还有其他方法吗?也许有些超时或什么?