1

Trying to make the try to run in a loop, since I am booting a machine containing the webserver and I want to make it run and not just go direct to the except and stop the script. I have made a while for the http-status code, but that does only work if the machine is up.

So my question is how can I make the try loop for like 5 minutes before it goes to the except? Sorry for my poor explanation.

try: r = requests.head("http://www.testing.co.uk") while r.status_code != 200: print "Response not == to 200." time.sleep(30) r = requests.head("http://www.testing.co.uk") else: print "Response is 200 - OK" except requests.ConnectionError: print "NOT FOUND - ERROR" 
11
  • If you're sleeping for 30 seconds and you want to keep trying for 5 minutes, you'll have to loop 10 times, right ? Commented Mar 11, 2015 at 18:36
  • If the connection is refused, you get an exception. Did you mean you wanted to keep on looping for 5 minutes before giving up altogether? Commented Mar 11, 2015 at 18:37
  • Correct, because it goes straight to the exception if the webserver is down. So I want to make it loop for 5 minutes for before it goes the exception. @MartijnPieters Commented Mar 11, 2015 at 18:39
  • The loop for checking http-code isn't the problem, but the exception, since it goes direct to the exception and stops if the webserver isn't up. I want to make it loop for 5 minutes before it stops. @alfasin Commented Mar 11, 2015 at 18:40
  • @Iknowpython: no, you cannot postpone the exception. I think you want to keep looping even if there was an exception each time. You want to retry for 5 minutes when there is an exception. Commented Mar 11, 2015 at 18:45

1 Answer 1

1

You could do something like:

import requests, time, datetime # Determine "end" time -- in this case, 5 minutes from now t_end = datetime.datetime.now() + datetime.timedelta(minutes=5) while True: try: r = requests.head("http://www.testing.co.uk") if r.status_code != 200: # Do something print "Response not == to 200." else: # Do something else print "Response is 200 - OK" break # Per comments time.sleep(30) # Wait 30 seconds between requests except requests.ConnectionError as e: print "NOT FOUND - ERROR" # If the time is past the end time, re-raise the exception if datetime.datetime.now() > t_end: raise e time.sleep(30) # Wait 30 seconds between requests 

The important line is:

if datetime.datetime.now() > t_end: raise e 

If the condition isn't met (less that 5 minutes have elapsed), the exception is silently ignored and the while loop continues.

If the condition is met, the exception is re-raised to be handled by some other, outer code or not handled at all -- in which case you'll see the exception "break" (in your words) the program.

The benefit of using this approach over something like (instead of while True:):

while datetime.datetime.now() > t_end: 

is that if you find yourself outside of the while loop, you know you got there from break and not from 5 minutes elapsing. You also preserve the exception in case you want to do something special in that case.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks that works. How can I get it out of the loop if it gets http-code 200?
Just add break inside the else block
@Iknowpython updated the answer to show you the break statement
Big Thanks for your help and making me understand how to do it.
@Iknowpython its likely going to a different exception, that block only catches exceptions of type requests.ConnectionError -- is that what you're getting?