[Python-Dev] Adding timeout to socket.py and httplib.py

Alan Kennedy python-dev at alan.kennedy.name
Tue Mar 20 23:21:30 CET 2007

> But now the result could be either an error code OR a socket.  One of
> the reasons to provide a timeout for the create_connection call, if I
> understand correctly, is to handle cases for which you don't get a
> response back in sufficient time.

AFAICT, that's the only reason. It's not to handle blocking sockets,
that's the default operation of sockets. And it's not to handle
non-blocking sockets either.

>If the user provides zero as a
> timeout, then they may very well get an exception, which is what they
> should expect.

Yes, they should expect it. And they would handle it like this

  new_socket = socket.create_connection(address, 0):
except socket.error:
  import errno:
  if errno.errno == 10035 # or relevant platform specific symbolic constant
    # socket is still connecting
    # there was a real socket error

> Then again, even with an arbitrary timeout, an exception
> is possible (especially if a host is down, etc.), and hiding the
> exceptional condition (didn't connect in the allotted time) is a bad
> thing.

See above.



More information about the Python-Dev mailing list