Waiting for receiving data

Gerald Walker geraldwalkerx at gmail.com
Tue Nov 24 03:15:10 CET 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)

Oops, the above doesn't run. This version works:

import socket
import urllib2

# 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 = urllib2.urlopen(xml_source)
except urllib2.URLError, e:
  print 'URLError = ' + str(e.reason)




More information about the Python-list mailing list