0

I am working with a while loop which I am trying to run for 5 minutes and check if it gets the http response code 200. The problem is if it doesn't get 200 it will be keep running, but it doesn't go to the else when the site should be up and running.

r = requests.head("http://www.testing.co.uk")
while r.status_code != 200:
    print "Response not == to 200."
    time.sleep(30)
else:
    print "Response is 200 - OK"
4
  • 1
    So what's the problem? Commented Mar 11, 2015 at 9:55
  • 2
    The status_code will not change unless you repeat the request! Commented Mar 11, 2015 at 9:55
  • When it first doesn't get 200, it will keep running the loop, still if the site is up and running after 50 seconds. Commented Mar 11, 2015 at 9:56
  • You should try to give number of attempts in while loop like while count < 5: than count -= instead of calling it infinitely. Commented Mar 11, 2015 at 9:59

3 Answers 3

4

Repeat the request.

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"
Sign up to request clarification or add additional context in comments.

5 Comments

I didn't know that while loops could contain else clauses until now, but I don't really understand the usefulness of that. Can't you just put the print without an else as loop would already be broken?
useless while..else, code duplication (request twice)
How can I give a maximum attemps ? If it runs every 30 seconds and should stop after 5min ( 10 attempts)
Just use a counter and add it to the condition of your for loop.
@Iknowpython: You should use time stamps to determine when to timeout. Using a fixed number of attempts might be too inaccurate, eg. what if each request hangs for 60 seconds and then times out (not all requests fail immediately!)? 10 of those, plus the 30 second interval will actually take about 15 minutes.
0

Try this which includes timeout checking up to approximately 5 minutes:

import time
from datetime import datetime, timedelta
import requests

FIVE_MINUTES = timedelta(minutes=1) * 5
RETRY_DELAY = 30

end_time = datetime.now() + FIVE_MINUTES

while datetime.now() < end_time:
    r = requests.head("http://www.testing.co.uk")
    if r.status_code == 200:
        print "Response is 200 - OK"
        break
    else:
        print "Response not == to 200."
        time.sleep(RETRY_DELAY)
else:
    print 'Timed out'

N.B approximately because each request will take an unpredictable period of time, e.g. a request might hang for 2 minutes, while other requests might fail immediately. Also, you should add exception handling because requests raise exceptions for failures such as "Connection refused".

2 Comments

Is it possible to loop the "except requests.ConnectionError:" because if it the server is not up the script will stop.
Yes. Use a try/except statement and execute your head request within it. Add an exception handler for each exception that you want to catch.
0

the request should be looped:

numOfRequests = 1000
for i in range(numOfRequests):
    r = requests.head("http://www.testing.co.uk")
    if r.status_code == 200:
        print "Response is 200 - OK"
        break
    print "Response not == to 200."
    time.sleep(30)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.