tags:

views:

1257

answers:

2

In Python 2.6, a new "timeout" parameter was added to the httplib.HTTPConnection class: http://docs.python.org/library/httplib.html#httplib.HTTPConnection

However, this is only a timeout for the connection to a server. I am looking to set a timeout value for the request, not the connection. This doesn't seem to be supported by httplib.

Is there any way to emulate this behavior?

+3  A: 

You can set a global socket timeout:

import socket

timeout = 10
socket.setdefaulttimeout(timeout)
Tomalak
if I set a socket timeout, will all HTTP requests sent from httplib timeout after the value I set?
Corey Goldberg
This should affect *all* socket operations.
Tomalak
Beware of HTTPS requests. The client might get stuck indefinitely because of a known bug in 2.6.x http://bugs.python.org/issue5103 (timeout is ignored during the handshake process in SSL)
bugspy.net
A: 

No, there isn't.

It's because the HTTP spec does not provide anything for the client to specify time-to-live information with a HTTP request. You can do this only on TCP level, as you mentioned.

On the other hand, the server may inform the client about timeout situations with HTTP status codes 408 Request Timeout resp. 504 Gateway Timeout.

mkoeller