I can only assume that the timeout is reached and it raises a timeout exception which I find odd as I cannot reproduce that, the connection simply closes if I create a socket and let it timeout.. Here's a quote from the docs of gevent.socket.create_connection:
Passing the optional timeout parameter will set the timeout on the
socket instance before attempting to connect. If no timeout is supplied, the global default timeout setting returned by getdefaulttimeout() is used.
getdefaulttimeout()
defaults to None
which means the socket will have no timeout.
You should always loop until you've received all of the data or until socket.recv()
returns an empty string. This indicates that the connection has been closed (for whatever reason). This applies to gevent sockets aswell.
I quote the socket howto:
When a recv returns 0 bytes, it means the other side has closed (or is in the process of closing) the connection. You will not receive any more data on this connection. Ever.
Your loop should look like this to handle the empty string case:
self.socket = gevent.socket.create_connection(self.address) # if timeout is necessary, make sure you add a high enough value.
payload = ""
while len(payload) < length:
b = self.socket.recv(length - len(payload))
if not b:
break # or return, or raise an exception.
payload += b