views:

376

answers:

5

By using python, how can I check if a website is up? From what I read, I need to check the "HTTP HEAD" and see status code "200 OK", but how to do so ?

Cheers

Related

+1  A: 

If by up, you simply mean "the server is serving", then you could use cURL, and if you get a response than it's up.

I can't give you specific advice because I'm not a python programmer, however here is a link to pycurl http://pycurl.sourceforge.net/.

Tyler Smith
+3  A: 

The HTTPConnection object from the httplib module in the standard library will probably do the trick for you. BTW, if you start doing anything advanced with HTTP in Python, be sure to check out httplib2; it's a great library.

Hank Gay
+11  A: 

You could try to do this with getcode() from urllib

>>> print urllib.urlopen("http://www.stackoverflow.com").getcode()
>>> 200
Anthony Forloney
Following question, using `urlopen.getcode` does fetch the entire page or not?
OscarRyz
As far as i know, `getcode` retreives the status from the response that is sent back
Anthony Forloney
@Oscar, there's nothing in urllib to indicate it uses HEAD instead of GET, but the duplicate question referenced by Daniel above shows how to do the former.
Peter Hansen
+1 didn't even read the answer :)
Dead account
+4  A: 

You can use httplib

import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("HEAD", "/")
r1 = conn.getresponse()
print r1.status, r1.reason

prints

200 OK

Of course, only if www.python.org is up.

OscarRyz
+2  A: 
import httplib
import socket
import re

def is_website_online(host):
    """ This function checks to see if a website is available by checking
        for socket info. If the website gets something in return, 
        we know it's available.
    """
    try:
        socket.gethostbyname(host)
    except socket.gaierror:
        return False
    else:
        return True


def is_page_available(host, path="/"):
    """ This function retreives the status code of a website by requesting
        HEAD data from the host. This means that it only requests the headers.
        If the host cannot be reached or something else goes wrong, it returns
        False.
    """
    try:
        conn = httplib.HTTPConnection(host)
        conn.request("HEAD", path)
        if re.match("^[23]\d\d$", str(conn.getresponse().status)):
            return True
    except StandardError:
        return None
Evan Fosmark
`is_website_online` just tells you if a host name has a DNS entry, not whether a website is online.
Craig McQueen
Whoops, you're right Craig. I'll update it in a bit.
Evan Fosmark