Grabbing HTTP headers with Python [ 7 lines of code ]

less than 1 minute read

As I mentioned in one of my previous post, I started with learning Python.

Here is one of the first scripts I wrote. It grabs the HTTP Response header. Great thing is that it’s possible to do this with only 7 lines of code.

Script code:

import urllib2
import sys
url = raw_input("Full url:")
header = urllib2.urlopen(url).info()

Example usage:

$>Full url:

As a result, you should get something like:

Date: Wed, 13 Mar 2013 12:35:43 GMT
Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimi
X-Powered-By: PHP/5.2.9
Vary: Accept-Encoding,Cookie
Cache-Control: max-age=3, must-revalidate
WP-Super-Cache: Served supercache file from PHP
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

If you liked the post, we should get connected - follow me on Twitter

1 Comment Grabbing HTTP headers with Python [ 7 lines of code ]

Leave a Reply

Your email address will not be published. Required fields are marked *