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"
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • If you're sleeping for 30 seconds and you want to keep trying for 5 minutes, you'll have to loop 10 times, right ? – Nir Alfasi Mar 11 '15 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? – Martijn Pieters Mar 11 '15 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 –  Mar 11 '15 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 –  Mar 11 '15 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. – Martijn Pieters Mar 11 '15 at 18:45
  • @MartijnPieters can you reopen this? The code has more errors than listed in the referenced question and will surely lead to a follow-up -- I have an answer that does a good job describing a better pattern. – jedwards Mar 11 '15 at 18:51
  • So there is no way to loop for example 5 minutes before it breaks ? –  Mar 11 '15 at 18:51
  • You can catch the exception, and inside the `except` block, decide whether to ignore it, or re-throw it, "breaking" it. – jedwards Mar 11 '15 at 18:53
  • Could you please show how? @jedwards –  Mar 11 '15 at 18:55
  • Do I have to have a except? without it it wont run. –  Mar 11 '15 at 18:58
  • @jedwards: reopened, with the right reserved to have it re-closed. – Martijn Pieters Mar 11 '15 at 18:59

1 Answers1

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.

jedwards
  • 29,432
  • 3
  • 65
  • 92