Python ->
Python connectivity test for number of web pages

The following example executes batch of HTTPConnection requests to number of web pages listed in text file. The Text file (web_pages_to_test.txt) is stored in the current directory together with .py script. Response results are written to log file (test_log.txt). This script uses [httplib] and [urlparse] modules.

# ------------------------------------------------------
# run connectivity tests to number of web pages
# ------------------------------------------------------

import datetime
import httplib
from urlparse import urlparse


def check_url(url):
   p = urlparse(url)
   conn = httplib.HTTPConnection(p.netloc, timeout=10)
   try:
     conn.request('HEAD', p.path)
     resp = conn.getresponse()
     return resp.status
   except:
     return 1

def check_url_verbose(url):
   p = urlparse(url)
   conn = httplib.HTTPConnection(p.netloc, timeout=10)
   try:
     conn.request('HEAD', p.path)
     resp = conn.getresponse()
     return resp.reason
   except:
     return 'Connection Failed !'


def main():
   # main begin

   print
   print 'Connectivity test started: {0}'.format(datetime.datetime.now())
   print '-----------------------------------------'
   print 'writing results to test_log.txt'

   # open log file for writing
   log_file = open('test_log.txt', 'w')
   # open data file for reading
   data_file = open('web_pages_to_test.txt')
   n = 0

   # iterate over the list of url addresses
   # read lines from .txt file into var the_url
   while True:

     the_url = data_file.readline()
     the_url = the_url.strip()

     # zero length indicates EOF
     if len(the_url) == 0:
       break
     else:
       # write output of check-url tests line to log file
       log_file.write(the_url + '\n')
       log_file.write('code: {0} message: {1}'.format(check_url(the_url),
             check_url_verbose(the_url)) + '\n')
       n += 1

   # close data file
   data_file.close()
   # close log file
   log_file.close()

   print 'total lines: {0}'.format(n)
   print '-----------------------------------------'
   print 'Connectivity test finished: {0}'.format(datetime.datetime.now())

   # main end


if __name__ == "__main__": main()




sqlexamples.info