Timur Izhbulatov — Independent Electronic Music

Python urllib2 exceptions

Sun, 17 May 2009 11:53:00 in Tech stuff | permalink

last.fm python urllib2

Yesterday I made a working prototype of my Last.fm stream ripper written in Python. This is my second project that relatively intensively uses urllib2 from Python standard library.

One of important things that I've learned about urllib2.urlopen() from my previous project is that it does raise some other exceptions besides urllib2.URLError (e.g. http.HTTPException and socket.error) which is the only one mentioned in the documentation.

Today I also found out that I'm not alone. Here's a discussion of this issue.

The summary of this discussion is that you need to handle (IOError, socket.error, httplib.HTTPException, ValueError) (in Python 2.6 socket.error is not needed, as it inherits from IOError). Also, ValueError should be handled in separate except statement, because it can be fixed and/or indicates a probable urrlib2 bug.