I'm trying to parse the location header of an HTTP response that is returned after using urllib2.urlopen, but the only response headers that I receive are from the target redirect --- not the original response that contains the location header.
I have followed other questions on Stack Overflow that suggest to subclass the urllib2.HTTPRedirectHandler, but I'm still not able to understand how to access the original response that urlopen ends up following.
Here's an example of the problem:
import urllib2
req = urllib2.urlopen("http://wp.me")
print req.info()
The output of print contains the response headers of the target of the redirected request. I would like to see the original.
Any help would be appreciated.
urllib2 does a transparent redirection, but as you said, you can subclass HTTPRedirectHandler and use that as an opener to get your required values.
Any further attributes that you can set for your req in the SmartRedirectHandler, can be made available to you via the result.