HTTP Request Timeout

2019-01-18 17:15发布

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?

标签: python http
4条回答
在下西门庆
2楼-- · 2019-01-18 17:18

You can set a global socket timeout (*):

import socket

timeout = 10
socket.setdefaulttimeout(timeout)

(*) EDIT: As people in the comments correctly point out: This is technically true, but it only has predictable results for tasks that involve a single socket operation. An HTTP request consists of multiple socket operations (e.g. DNS requests or other things that might be abstracted away from an HTTP client). The timeout of the overall operation becomes unpredictable because of that.

查看更多
女痞
3楼-- · 2019-01-18 17:28

When you initiate the connection, it is posible to give as third argument the timout value.

connection = HTTPConnection(<IP of URL>, <port or None>, <timeout>)
...

is a float in seconds.

connection = HTTPConnection('slow.service.com', None, 20.0)
查看更多
看我几分像从前
4楼-- · 2019-01-18 17:41

You could also use settimeout on the socket of the connection (works with Python 2.5):

connection = HTTPConnection('slow.service.com')
connection.request(...)
connection.sock.settimeout(5.0)
response = connection.getresponse()
response.read()
connection.close()

If the server cannot send the response within 5 seconds, a socket.error will be raised.

查看更多
我命由我不由天
5楼-- · 2019-01-18 17:43

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.

查看更多
登录 后发表回答