> Is there any way to fetch a website's host/version headers using[color=blue]
> Python?[/color]
[color=blue][color=green][color=darkred]
>>> import httplib
>>> conn = httplib.HTTPCon nection("docs.p ython.org")
>>> conn.connect()
>>> conn.request("H EAD", "/")
>>> response = dict([(k.lower(), v) for k,v in conn.getrespons e()])
>>> conn.close()
>>> server = response["server"]
>>> print server[/color][/color][/color]
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
I've found a bit of discrepancy with regards to the case of the
"server" portion, so the above code just normalizes it to
lowercase and then shoves it in a dictionary.
You can then do as you please with the contents of the "server"
variable.
It's theoretically possible that the server can return differing
headers based on the URL you request or its method. You'll have
to adjust the request() call for the method (GET/HEAD/POST, etc)
and for the resource you want (in this case, just "/")
>> Is there any way to fetch a website's host/version headers using[color=blue][color=green]
>> Python?[/color]
>[color=green][color=darkred]
> >>> import httplib
> >>> conn = httplib.HTTPCon nection("docs.p ython.org")
> >>> conn.connect()
> >>> conn.request("H EAD", "/")
> >>> response = dict([(k.lower(), v) for k,v in conn.getrespons e()])
> >>> conn.close()
> >>> server = response["server"]
> >>> print server[/color][/color]
> 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[/color]
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...
[color=blue][color=green][color=darkred]
>>> import httplib
>>> conn = httplib.HTTPCon nection("docs.p ython.org")
>>> conn.request("H EAD", "/")
>>> response = conn.getrespons e()
>>> conn.close()
>>> server = response.gethea der("server")
>>> print server[/color][/color][/color]
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.
Comment