Python 用socket模块实现检测端口和检测web服务(二)

2014-11-24 03:17:14 · 作者: · 浏览: 8
check = check_webserver(options.address, options.port, options.resource)


print 'check_webserver returned %s' % check


sys.exit(not check)


运行结果:


正常


[root@centos python]# python check_web.py -a 192.168.137.2 -p 80 -r index.html


options: {'resource': 'index.html', 'port': 80, 'address': '192.168.137.2'}, args: []


HTTP request:


|||GET /index.html HTTP/1.1


Host: 192.168.137.2


|||


Attempting to connect to 192.168.137.2 on port 80


Connected to 192.168.137.2 on port 80


Received 100 bytes of HTTP response


|||HTTP/1.1 200 OK


Date: Wed, 19 Jun 2013 02:29:31 GMT


Server: Apache/2.2.3 (CentOS)


Last-Modified: |||


Closing the connection


First line of HTTP response: HTTP/1.1 200 OK


Version: HTTP/1.1, Stuats: 200, Message: OK


Success - status was 200


check_webserver returned True


不正常,找不到页面的情况


[root@centos python]# python check_web.py -a 192.168.137.2 -p 80 -r ppp.html


options: {'resource': 'ppp.html', 'port': 80, 'address': '192.168.137.2'}, args: []


HTTP request:


|||GET /ppp.html HTTP/1.1


Host: 192.168.137.2


|||


Attempting to connect to 192.168.137.2 on port 80


Connected to 192.168.137.2 on port 80


Received 100 bytes of HTTP response


|||HTTP/1.1 404 Not Found


Date: Wed, 19 Jun 2013 02:29:41 GMT


Server: Apache/2.2.3 (CentOS)


Content-|||


Closing the connection


First line of HTTP response: HTTP/1.1 404 Not Found


Version: HTTP/1.1, Stuats: 404, Message: Not Found


Staus was 404


check_webserver returned False