Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a timeout control for urlllib2.urlopen

How to implement a controlling for urlllib2.urlopen in Python ? I just wanna monitor that if in 5 seconds no xml data return, cut this connection and connect again? Should I use some timer? thx

like image 593
user2263305 Avatar asked Apr 15 '13 14:04

user2263305


2 Answers

urllib2.urlopen("http://www.example.com", timeout=5)
like image 121
Yarkee Avatar answered Oct 19 '22 06:10

Yarkee


From the urllib2 documentation...

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections.

like image 37
Aya Avatar answered Oct 19 '22 07:10

Aya