user2263305
user2263305

Reputation: 97

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

Upvotes: 5

Views: 13822

Answers (2)

Yarkee
Yarkee

Reputation: 9424

urllib2.urlopen("http://www.example.com", timeout=5)

Upvotes: 14

Aya
Aya

Reputation: 42080

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.

Upvotes: 7

Related Questions