-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathchkurl.py
executable file
·45 lines (39 loc) · 1.53 KB
/
chkurl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python
import httplib
import sys
def check_webserver(address, port, resource):
#create connection
if not resource.startswith('/'):
resource = '/' + resource
try:
conn = httplib.HTTPConnection(address, port)
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 sock. error, e:
print ' HTTP connection failed: %s' % e
return False
finally:
conn. close()
print ' HTTP connection closed successfully'
if response. status in [200, 301]:
return True
else:
return False
if __name__ == ' __main__':
from optparse import 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 to check", 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)