• Configurable multiple tries and maximum failures
  • Randomly pick testsites from a (huge) list of urls
  • The url list can contain any site
  • Platform independent
  • Tested with Python 2.5.2

  • You need an url list (it takes 5 minute searching “top 500 website” on any search engine)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python

import time, random, mysock
from httplib import BadStatusLine
from urllib2 import urlopen, URLError

def test_connection(times=(2,3), rec=False):
    urls = ("http://1", "http://2", "http://3"
            )
    if rec == False:
        if times[0] < 0 or times[1] < 1 or times[0] > times[1]: return False
        failed = 0
        for i in range(times[1]):
            if test_connection(0, True) == False:
                failed += 1
                print time.ctime() + " " +  "WARNING: Failed " + str(failed) +" out of " + str(times[1]) + " connection (more than " + str(times[0]) +" out of " + str(times[1]) +") causes an error."
            else: return True
        if failed >= (times[1] - times[0]):
            delay = 5
            print time.ctime() + " " +  "ERROR: Connection lost, sleeping " + delay + " minutes."
            time.sleep(delay * 60)
            return False
        else: return True
    else:
        try:
            url = random.choice(urls)
            print time.ctime() + " " +  "Testing connection using \"" + url + "\"."
            mysock = urlopen(url)
            #testc = mysock.read()
            mysock.close()
        except (URLError, BadStatusLine), err:
            return False
        return True

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
(License)