Python urllib2 Response header

Viewed 58192

I'm trying to extract the response header of a URL request. When I use firebug to analyze the response output of a URL request, it returns:

Content-Type text/html

However when I use the python code:

urllib2.urlopen(URL).info()

the resulting output returns:

Content-Type: video/x-flv

I am new to python, and to web programming in general; any helpful insight is much appreciated. Also, if more info is needed please let me know.

Thanks in advance for reading this post

6 Answers

for getting raw data for the headers in python2, a little bit of a hack but it works.

"".join(urllib2.urlopen("http://google.com/").info().__dict__["headers"])

basically "".join(list) will the list of headers, which all include "\n" at the end.

__dict__ is a built in python variable for all dicts, basically you can select a list out of a 2d array with it.

and ofcourse ["headers"] is selecting the list value from the .info() response value dict

hope this helped you learn a few ez python tricks :)

Related