-
Notifications
You must be signed in to change notification settings - Fork 25
/
url_check.py
executable file
·60 lines (52 loc) · 1.52 KB
/
url_check.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
"""
1. It takes arguments for: timeout, url, expected_response_code, retry_count
2. The script performs an HTTP GET for the url
3. If the HTTP response code matches the expected_response_code the script will exit with status code 0
4. If the the HTTP GET either times out or the HTTP response code doesn't match, retry until retry count is exhausted
5. If the retry count is exhausted without receiving the expected_response_code, print an error message to STDERR and exit with a non zero status code.
"""
import os, sys, httplib, socket
#
# verify number of arguments
#
args = len(sys.argv)
if not args == 5:
print "Usage: %s {timeout|url|expected_ response|retry_count}" % \
os.path.basename(sys.argv[0])
sys.exit(1)
#
# redirect stderr to null class
#
class NullWriter:
def write(self, s):
pass
sys.stderr = NullWriter()
#
# assign the args
#
TimeOut = (sys.argv[1])
site = (sys.argv[2])
ExpectResponse = (sys.argv[3])
RetryCount = (sys.argv[4])
#
# connect
#
i = 0
t = int(RetryCount)
while i < t:
c = str(t - i)
ConnectString = httplib.HTTPConnection(site, 80, TimeOut)
ConnectString.request("GET", "/")
Response = ConnectString.getresponse()
sock = socket.socket()
sock.settimeout(8.2)
ConnectString.close()
if Response.status == httplib.OK:
print "URL is OK"
sys.exit(0)
else:
print "URL did not connect as expected."
print "Will attempt the connection " + c + " more times."
i += 1
sys.exit(1)