Finding web host headers

H

Harlin Seritt

Is there any way to fetch a website's host/version headers using
Python?

Thanks,

Harlin
 
T

Tim Chase

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


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 "/")

-tkc
 
T

Tim Chase

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...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top