python socket 检查服务器端口及返回状态码
(2012-09-25 10:33:27)
标签:
育儿 |
分类: python |
[root@vm046 python.Uninx_Linux]# cat check_http.py
#!/usr/bin/python
import socket
import re
import sys
from optparse import OptionParser
def check_webserver(address,port,resource):
#build up HTTP request string
if not resource.startswith('/'):
resource = '/' + resource
request_string = "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n" %
(resource,address)
print 'HTTP request:'
print '|||%s|||' % request_string
#create a TCP socket
s = socket.socket()
print "Attempting to connect to %s on port %s" %
(address,port)
try:
s.connect((address,port))
print "Connected to %s on port %s" % (address,port)
s.send(request_string)
#we should only need the first 100 bytes or so
rsp = s.recv(100)
print 'Received 100 bytes of HTTP response'
print '|||%s|||' % rsp
except socket.error, e:
print "Connection to %s on port %s failed: %s" %
(address,port,e)
return False
#!/usr/bin/python
import socket
import re
import sys
from optparse import OptionParser
def check_webserver(address,port,resource):