Timeout on a HTTP request in python
-
22-08-2019 - |
Question
Very occasionally when making a http request, I am waiting for an age for a response that never comes. What is the recommended way to cancel this request after a reasonable period of time?
Solution
Set the HTTP request timeout.
OTHER TIPS
The timeout parameter to urllib2.urlopen, or httplib. The original urllib has no such convenient feature. You can also use an asynchronous HTTP client such as twisted.web.client, but that's probably not necessary.
If you are making a lot of HTTP requests, you can change this globally by calling socket.setdefaulttimeout
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow