Take the 2-minute tour ×

I've been searching all around for a Python 3.x code sample to get HTTP Header information.

Something as simple as get_headers equivalent in PHP cannot be found in Python easily. Or maybe I am not sure how to best wrap my head around it.

In essence, I would like to code something where I can see whether a URL exists or not

something in the line of

h = get_headers(url)
if(h[0] == 200)

So far, I tried

h = http.client.HTTPResponse('http://docs.python.org/')

But always got an error

share|improve this question
possible duplicate of Grabbing headers from webpage with python –  DocMax Feb 19 '13 at 5:36
@DocMax: Possible, but the accepted answer there doesn't deal with response codes or exception handling. –  Johnsyweb Feb 19 '13 at 6:06
Good point. I had overlooked this subtlety. To other reviewers, please keep this in mind. I would rescind my vote now were it possible. –  DocMax Feb 19 '13 at 6:27
add comment

3 Answers

up vote 3 down vote accepted

To get an HTTP response code in , use the urllib.request module:

>>> import urllib.request
>>> response =  urllib.request.urlopen(url)
>>> response.getcode()
>>> if response.getcode() == 200:
...     print('Bingo')

The returned HTTPResponse Object will give you access to all of the headers, as well. For example:

>>> response.getheader('Server')
'Apache/2.2.16 (Debian)'

If the call to urllib.request.urlopen() fails, an HTTPError Exception is raised. You can handle this to get the response code:

import urllib.request
    response = urllib.request.urlopen(url)
    if response.getcode() == 200:
        print('The response code was not 200, but: {}'.format(
except urllib.error.HTTPError as e:
    print('''An error occurred: {}
The response code was {}'''.format(e, e.getcode()))
share|improve this answer
Thank you! This is the answer. I got it after researching more based on the initial answer Ali-Akber provided. Thanks again! –  Adib Behjat Feb 19 '13 at 4:35
So, the danger with this code is that if the page is 404, it gives me a massive error ending with (urllib.error.HTTPError: HTTP Error 404: Not Found). What's a good way to go around that problem? –  Adib Behjat Feb 19 '13 at 4:57
I've updated my answer to demonstrate how to handle a urllib.error.HTTPError. –  Johnsyweb Feb 19 '13 at 5:17
add comment

For Python 2.x

urllib, urllib2 or httplib can be used here. However note, urllib and urllib2 uses httplib. Therefore, depending on whether you plan to do this check a lot (1000s of times), it would be better to use httplib. Additional documentation and examples are here.

Example code:

import httplib
    h = httplib.HTTPConnection("www.google.com")
except Exception as ex:
    print "Could not connect to page."

For Python 3.x

A similar story to urllib (or urllib2) and httplib from Python 2.x applies to the urllib2 and http.client libraries in Python 3.x. Again, http.client should be quicker. For more documentation and examples look here.

Example code:

import http.client

    conn = http.client.HTTPConnection("www.google.com")
except Exception as ex:
    print("Could not connect to page.")

and if you wanted to check the status codes you would need to replace



conn.request("GET", "/index.html")  # Could also use "HEAD" instead of "GET".
res = conn.getresponse()
if res.status == 200 or res.status == 302:  # Specify codes here.
    print("Page Found!")

Note, in both examples, if you would like to catch the specific exception relating to when the URL doesn't exist, rather than all of them, catch the socket.gaierror exception instead (see the socket documentation).

share|improve this answer
Thanks for this answer. For the current project, I think the URLLIB would work. But once I go through examining many urls at once, I might use your method. Thanks again! –  Adib Behjat Feb 19 '13 at 4:36
So, I realized that this does not work on Python 3.x either. –  Adib Behjat Feb 19 '13 at 4:45
I've added a Python 3.x solution. Should work now - it's not much different to the original answer as you can see. –  Cleros Feb 19 '13 at 18:12
Thanks! This informative. I've went through the documentation in the Python 3.3 library. But I guess the problem I originally had was finding the right syntax to implement since I would get confused with the Python 2.x vs Python 3.x functions and calls. –  Adib Behjat Feb 19 '13 at 21:06
add comment

you can use the urllib2 library

import urllib2
if urllib2.urlopen(url).code == 200:
    print "Bingo"
share|improve this answer
This doesn't work in Python 3. –  kuyan Feb 19 '13 at 4:26
This is fine for Python 2. The question is tagged with python-3.x, though. –  Johnsyweb Feb 19 '13 at 4:27
This is for Python 2.x, but it did help a lot! Thank you –  Adib Behjat Feb 19 '13 at 4:33
add comment

Your Answer


