Checking if a website is up via Python

Hellnar picture Hellnar · Dec 22, 2009 · Viewed 126.1k times · Source

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

Answer

Anthony Forloney picture Anthony Forloney · Dec 22, 2009

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

>>> print urllib.urlopen("http://www.stackoverflow.com").getcode()
>>> 200

EDIT: For more modern python, i.e. python3, use:

import urllib.request
print(urllib.request.urlopen("http://www.stackoverflow.com").getcode())
>>> 200