us line'
return False
if status in ['200','301']:
print 'Success - status was %s' %status
return True
else:
print 'Status was %s' %status
return False
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-a","--address",dest="address",default='localhost',help="ADDRESS for webserver",metavar="ADDRESS")
parser.add_option("-p","--port",dest="port",type="int",default=80,help="PORT for webserver",metavar="PORT")
parser.add_option("-r","--resource",dest="resource",default='index.html',help="RESOURCE for webserver",metavar="RESOURCE")
(options,args)=parser.parse_args()
print 'options: %s,args: %s' %(options,args)
check=check_webserver(options.address,options.port,options.resource)
print 'check_webserver returned %s' %check
sys.exit(not check)
以上程序主要通过check_webserver函数去探测HTTP服务,检测端口是否可以连接,并发送HTTP请求,通过web server的返回值进行判断。
$ python tcp_port_checker2.py -a 10.10.41.20 -p 80 -r index.php
options: {'resource': 'index.php', 'port': 80, 'address': '10.10.41.20'},args: []
HTTP request:
|||GET /index.php HTTP/1.1
Host: 10.10.41.20
|||
Attempting to connect to 10.10.41.20 on port 80
Connected to 10.10.41.20 on port 80
Received 100 bytes of HTTP response
|||HTTP/1.1 404 Not Found
Server: nginx
Date: Tue, 22 Jul 2014 03:24:17 GMT
|||tent-Type: text/html
Closing the connection
First line of HTTP response: HTTP/1.1 404 Not Found
Version: HTTP/1.1, Status: 404, Message: Not Found
Status was 404
check_webserver returned False