views:

168

answers:

3

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?

+2  A: 

Set the HTTP request timeout.

jcoon
I must be pretty tired, not to think to look there first, Thanks!
wodemoneke
+1  A: 

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.

Devin Jeanpierre
+1  A: 

If you are making a lot of HTTP requests, you can change this globally by calling socket.setdefaulttimeout

Sebastian Celis
Thanks, I'm doing this because it seems that the timeout parameter was only added in python 2.6 and I need to support 2.5 as. well
wodemoneke