Finding web host headers

Tim Chase python.list at tim.thechases.com
Thu Jun 1 13:20:44 EDT 2006


>> Is there any way to fetch a website's host/version headers using
>> Python?
> 
>  >>> import httplib
>  >>> conn = httplib.HTTPConnection("docs.python.org")
>  >>> conn.connect()
>  >>> conn.request("HEAD", "/")
>  >>> response = dict([(k.lower(), v) for k,v in conn.getresponse()])
>  >>> conn.close()
>  >>> server = response["server"]
>  >>> print server
> Apache/2.0.54 (Debian GNU/Linux) DAV/2 SVN/1.1.4 mod_python/3.1.3 
> Python/2.3.5 mod_ssl/2.0.54 OpenSSL/0.9.7e

Dang, I copied that over by hand and miscopied it with a big 
error or two.  It can also be cleaned up a bit, as I learned (the 
getheader() call is case-insensitive, and the connect() call was 
superfluous).  Copying verbatim...

 >>> import httplib
 >>> conn = httplib.HTTPConnection("docs.python.org")
 >>> conn.request("HEAD", "/")
 >>> response = conn.getresponse()
 >>> conn.close()
 >>> server = response.getheader("server")
 >>> print server
Apache/2.0.54 (Debian GNU/Linux) DAV/2 SVN/1.1.4 mod_python/3.1.3 
Python/2.3.5 mod_ssl/2.0.54 OpenSSL/0.9.7e



Sorry about the rubbish code the first time out the gate.

-tkc





More information about the Python-list mailing list