Finding web host headers

Discussion in 'Python' started by Harlin Seritt, Jun 1, 2006.

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

    Thanks,

    Harlin
     
    Harlin Seritt, Jun 1, 2006
    #1
    1. Advertising

  2. Harlin Seritt

    Tim Chase Guest

    > 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
     
    Tim Chase, Jun 1, 2006
    #2
    1. Advertising

  3. Harlin Seritt

    Tim Chase Guest

    >> 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
     
    Tim Chase, Jun 1, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Easterly
    Replies:
    3
    Views:
    388
    Karl Easterly
    Aug 20, 2003
  2. dont bother
    Replies:
    0
    Views:
    847
    dont bother
    Mar 3, 2004
  3. VMI
    Replies:
    4
    Views:
    281
    Munna
    Aug 5, 2008
  4. Phil
    Replies:
    4
    Views:
    716
    Gabriel Genellina
    Jan 17, 2010
  5. Ian
    Replies:
    2
    Views:
    2,073
Loading...

Share This Page