HTTP Response Header Information in Python

Sometimes we need to get the HTTP Response header from our program. You can check the following python code, that prints the http response header:

>>> import urllib2
>>> usock = urllib2.urlopen('http://bdosn.org')
>>> print usock.info()
Date: Thu, 24 Jul 2008 19:52:53 GMT
Server: Apache/1.3.39 (Unix) PHP/5.2.3 mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 mod_gzip/1.3.26.1a FrontPage/5.0.2.2634a mod_ssl/2.8.30 OpenSSL/0.9.7a
X-Powered-By: PHP/5.2.3
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

>>>

If you want to get the value of a particular item, use the get() method:

>>> print usock.info().get('Date')
Thu, 24 Jul 2008 19:52:53 GMT
>>>


Hope you will find this tips useful!

Comments

David said…
Brilliant! Just what I was looking for.
AB said…
Me too!
Thanks
Anonymous said…
unfortunately, this always follows redirects, so you can't get the header of the original page

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code