简体   繁体   English

Python循环,直到请求响应代码为200

[英]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. 我正在尝试发出一个python请求,并在响应状态代码为200时继续执行脚本。否则,请继续循环直到我得到200。

Tried the following statements not sure what I am missing here. 试过以下陈述,不知道我在这里缺少什么。 The condition does not exit the loop. 条件不会退出循环。

Try 1: 试试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: 试试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: 尝试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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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