DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Is Site Down Script

01.17.2008
| 5277 views |
  • submit to reddit
        // Tests if site is down/server is offline by trying to connect in specified time

import socket
import urllib2

def timeout(site, timeout):
  save = socket.getdefaulttimeout() 
  try:
    response = urllib2.urlopen(site)
    socket.setdefaulttimeout(save)
  except urllib2.URLError, err:
    socket.setdefaulttimeout(save)
    if err.__class__.__name__ == "URLError":
      if isinstance(err[0], socket.timeout):
        return True
  return False


if timeout("http://www.dummy-site.com/test_timeout.html", 10):
  print "Timeout detected"