Flangy > Software Development > Web Note: Handling other redirects with urllib2
Python's urllib2 module has a simple HTTP handler. It doesn't do all possible HTTP 1.1 response codes, which is fine, but there are some (at least 1) that could be handled by reusing existing code. Specifically, it looks like 307 Tempory Redirect can be handled the same way as 302. I'd guess that 303 See Other could use the 302 handler too.
Not that these response codes are used that often. But I was helping someone in #python and whatever server he was trying to pull from returned a 307 on certain URLs. Here's how to get urllib2 to handle 307s too:
import urllib2 thing = urllib2.HTTPRedirectHandler() thing.http_error_307 = thing.http_error_302 opener = urllib2.build_opener(thing) # This URL gives me a 307 response code, at least, it did # when I wrote the script. url = 'http://www.cs.columbia.edu/~min/papers/jcdl02.pdf' page = opener.open(url) print page.info()