I got some code to receive data that looks like this: (SSL being used)
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
I am having problems when there are some significant delays, so I added those counters. My problem is that, if I don't know how big the message is, I don't know when to stop receiving. I used to break when the received amount was less than the buffer, but it appears that sometimes this happens anyway but not all the data has arrived. Are there any other ways? Maybe some timeout or something?