2

I am trying to make a python request and proceed with the script when the response status code is 200. Else, keep looping through until I get 200.

Tried the following statements not sure what I am missing here. The condition does not exit the loop.

Try 1:

while True:
    if (offense_response.status_code == 404):
        time.sleep(5)
        logging.info("Status code is 404, entering sleep for 5 seconds")
        offense_response = requests.get(qradar_offense_url, headers=q_headers, verify=False)
        continue
    if (offense_response.status_code == 200):
        logging.info("Status code is 200, exiting loop")
        exit()

Try 2:

while (offense_response.status_code != 200):
    if (offense_response.status_code == 404):
        logging.info("Status code is 404, entering sleep for 5 seconds")
        time.sleep(5)
        offense_response = requests.get(qradar_offense_url, headers=q_headers, verify=False)
    else: 
        logging.info("Status code is 200, exiting loop")

Try 3:

while True:
    if (offense_response.status_code != 200):
        time.sleep(5)
        logging.info("Checking Response Status Code again")
        offense_response = requests.get(qradar_offense_url, headers=q_headers, verify=False)
        if (offense_response.status_code == 200):
            break
5
  • 7
    I don't see a new request made. Do you need to retry the call and save the new value to offense_response? Commented May 9, 2019 at 18:56
  • 1
    @karthik where in these snippets are you updating/recalculating the offense_response.status_code after waking from sleep ? Commented May 9, 2019 at 18:58
  • Missed that point, added the retry calls. Still no luck. Commented May 9, 2019 at 19:13
  • why do you use parethesis in if with a single comparison? Commented May 9, 2019 at 19:17
  • If you get a 404 and then keep trying the same URL, chances are you'll keep getting a 404 and never finish the loop. Commented May 9, 2019 at 20:06

1 Answer 1

3

Can you try this one:

status = True
while status:
    if (offense_response.status_code != 200):
        #check the the status and assign to offense_response.status_code
        logging.info("Status code is not 200, entering sleep for 5 seconds")
        time.sleep(5)
    else:
        logging.info("status code is 200, hence exiting")
        status = False
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.