Waiting for receiving data

Gerald Walker geraldwalkerx at gmail.com
Mon Nov 23 21:01:55 EST 2009


> 
> import socket
> from urllib2 import urlopen
> 
> # A one-hundredths of a second (0.01) timeout before socket throws
> # an exception to demonstrate catching the timeout.
> # Obviously, this you will set this greater than 0.01 in real life.
> socket.setdefaulttimeout(0.01)
> 
> # example xml feed
> xml_source = "http://mlb.mlb.com/partnerxml/gen/news/rss/mlb.xml"
> 
> try:
>   data = urlopen(xml_source)
> except urllib2.URLError, e:
>   print 'URLError = ' + str(e.reason)

Also, if you are using multiple threads to retrieve the xml source(s)
and any thread blocks due to network problems, the thread can go way by
itself after the default timeout expires.





More information about the Python-list mailing list