Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Is Site Down Script

DZone's Guide to

Is Site Down Script

· ·
Free Resource
// 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"

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}