Reputation: 15
now it works once if connected successfully, but if exception is met, it doesn't retry as I wish, just throwing:
Will retry: [Errno 111] Connection refused
It should return False if all attempts weren't successful and True if at least one returned an answer
Seems there's something complicated with 'while' needed, like
for attempt in range(attempts) and while True
Here's my code:
attempts = 10
for attempt in range(attempts):
try:
conn = httplib.HTTPConnection("server:80", timeout=5)
conn.request("GET","/url")
r = conn.getresponse()
except socket.error, serr:
print("Will retry: %s" % serr)
conn.close()
else:
print("OK")
finally:
return False
I tried also:
for attempt in range(attempts):
while True:
try:
The same result...
Upvotes: 1
Views: 1998
Reputation: 1631
Try using a counter and a flag inside a while loop.
def funct():
flag = False
counter = 0
while True:
counter += 1
try:
conn = httplib.HTTPConnection("server:80", timeout=5)
conn.request("GET","/url")
r = conn.getresponse()
flag = True
break
except socket.error, serr:
print("Will retry: %s" % serr)
conn.close()
if counter>9:
break
return flag
Upvotes: 1