How to implement timeout control for urlllib2.urlopen

How to implement control for urlllib2.urlopen in Python? I just want to keep track of if after 5 seconds the XML data does not return, disconnect this connection and reconnect? Should I use some kind of timer? THX

+5
source share
2 answers
urllib2.urlopen("http://www.example.com", timeout=5)
+13
source

From the urllib2documentation ...

An additional timeout parameter defines the timeout in seconds for blocking operations, such as a connection attempt (if not specified, the global default timeout setting will be used). This actually works for HTTP, HTTPS and FTP connections.

+7
source

All Articles