Web request error handling

Added a try / except block around placing a urllib2.urlopen().
Additionally, when there is an error I print it and return None.  This
will tell the cache to remove the opened file.
This commit is contained in:
Bryan Schumaker 2010-11-02 21:08:37 -04:00
parent 5803f74701
commit 7308ed8225
2 changed files with 16 additions and 7 deletions

View File

@ -29,6 +29,8 @@ class LastFmRequest(dict):
for param in self:
url[param] = self[param]
file = url.open()
if file == None:
return None
if printfile==True:
self.print_file(file)
doc = xm.parse( file )
@ -43,6 +45,8 @@ def lfm_cache_album(file, artist, album):
req["album"] = album
req["artist"] = artist
doc = req.place()
if doc == None:
return False
node = pref_attr(doc, "image", "size", pref_sizes)
node = xm.child(node)
if node == None:

View File

@ -6,9 +6,10 @@ import string
HEADER = libsaria.__vers__
vals = [ ('%','25'), (' ','20'), ('&','26'), ('<','3C'), ('>','3E'),
('"','22'), ('#','23'), ('$','24'), ('\'','27'), ('+','2B'),
(',','2C'), ('/','2F'), (':','3A'), ('[','5B'), (']','5D')]
#vals = [ ('%','25'), (' ','20'), ('&','26'), ('<','3C'), ('>','3E'),
# ('"','22'), ('#','23'), ('$','24'), ('\'','27'), ('+','2B'),
# (',','2C'), ('/','2F'), (':','3A'), ('[','5B'), (']','5D')]
vals = [ (' ','20'), ('&','26') ]
class Url:
@ -20,10 +21,14 @@ class Url:
self.url += "&" + key + "=" + value
def open(self):
#print self.url
req = urllib2.Request(self.url)
req.add_header('User-Agent', HEADER)
return urllib2.urlopen(req)
print self.url
try:
req = urllib2.Request(self.url)
req.add_header('User-Agent', HEADER)
return urllib2.urlopen(req)
except Exception, e:
print e
return None
def escape(string):