#!/usr/bin/python2.6
import httplib
import sys
import subprocess
def check_webserver(address, port, resource):
time = subprocess.Popen("date +'%Y-%m-%d %H:%M:%S'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
date = time.stdout.read().rstrip()
#create connection
if not resource.startswith('/'):
resource = '/' + resource
try:
conn = httplib.HTTPConnection(address, port, timeout=6)
print 'HTTP connection created successfully'
#make request
req = conn.request('GET', resource)
print 'request for %s successful' % resource
#get response
response = conn.getresponse()
print 'response status: %s' % response.status
except:
err_rec=open('/root/wz/python/err_rec.txt', 'a')
#time = subprocess.Popen("date +'%Y-%m-%d %H:%M:%S'", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
#date = time.stdout.read().rstrip()
err_rec.write('HTTP connection failed: %s:%s %s\n' %(address, port, date))
err_rec.close()
print 'HTTP connection failed: %s:%s' % (address, port)
return False
finally:
conn.close()
print 'HTTP connection closed successfully'
if response.status in [200, 301]:
return True
else:
err_rec=open('/root/wz/python/err_rec.txt', 'a')
err_rec.write('HTTP response status: %s:%s %s %s\n' %(address, port, response.status, date))
err_rec.close()
if __name__ == '__main__':
page_list = open('/root/wz/python/page_list.txt', 'r')
for line in page_list:
host_port, res = line.rstrip().split("/")
host, port = host_port.split(":")
check_webserver(host, port, resource=res)
|