Skip to content

Commit

Permalink
python 3 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lord-vesel committed Dec 12, 2023
1 parent 74f3860 commit 4377bec
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions health_check → health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,20 @@
'''

from ansible.module_utils.basic import *
import httplib
#import httplib2
import re
import socket
import time
import urllib2
from urllib.error import URLError, HTTPError
from urllib.request import Request, urlopen


def check_server_status(url, headers, timeout, expected_status,
expected_regexp):
request = urllib2.Request(url, headers=headers)
request = Request(url, headers=headers)
try:
fp = urllib2.urlopen(request, timeout=timeout)
except (urllib2.URLError, httplib.HTTPException, socket.error), e:
fp = urlopen(request, timeout=timeout)
except (URLError, HTTPError, socket.error) as e:
return False, str(e)

if fp.getcode() != expected_status:
Expand Down Expand Up @@ -139,7 +140,7 @@ def main():

time.sleep(initial_delay)
info = ''
for attempt in xrange(max_retries):
for attempt in range(max_retries):
if attempt != 0:
time.sleep(delay_between_tries)
success, info = check_server_status(
Expand All @@ -152,5 +153,5 @@ def main():
module.fail_json(msg='Maximum attempts reached: ' + info,
failed_attempts=attempt)

main()

if __name__ == '__main__':
main()

0 comments on commit 4377bec

Please sign in to comment.