python 如果status_code为503,请重试

ujv3wf0j  于 2022-12-02  发布在  Python
关注(0)|答案(1)|浏览(152)
def verify_app_log_cur_day2(self, anypoint_monitoring, organization_id, int, applist, access_token):
    headers = {"Authorization": f"Bearer {access_token}"}
    payload = {}
    log_list = []
    for item in applist:
        url = f"{anypoint_monitoring}/organizations/{organization_id}/environments/{int}/applications/{item}/logs}"
        response = requests.request("GET", url, headers=headers, data=payload)
        if response.status_code == 503:
            continue
        else:
            log_list.append([response.json()])
    return log_list

如何为代码添加功能?

  • 如果响应状态代码== 503,则重试
  • 如果响应状态代码再次== 503,则继续
cyvaqqii

cyvaqqii1#

只需这样做

def verify_app_log_cur_day2(self, anypoint_monitoring, organization_id, int, applist, access_token):
    headers = {"Authorization": f"Bearer {access_token}"}
    payload = {}
    log_list = []
    for item in applist:
        url = f"{anypoint_monitoring}/organizations/{organization_id}/environments/{int}/applications/{item}/logs}"
        response = requests.request("GET", url, headers=headers, data=payload)

        # Functionality:
        if response.status_code == 503:
            response = requests.request("GET", url, headers=headers, data=payload)
            if response.status_code == 503:
                continue                      
        else:
            log_list.append(response.json())

    return log_list

相关问题