简体   繁体   中英

Python loop until request response code is 200

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM