首页 > 解决方案 > Python HTTPS请求:重试状态码返回API

问题描述

我尝试使用 Python 3.6 对某些 HTTP 状态和网络错误进行重试,并对外部 API 的请求进行重试。

我目前已经实现了这段代码遵循不同的指导方针:

https://dev.to/ssbozy/python-requests-with-retries-4p03

如何在python请求库中实现重试机制?

我的代码(仅重要部分)我没有在这里展示所有的尝试,除了代码

import requests
import logging
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from urllib3.exceptions import MaxRetryError

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s :%(levelname)s : %(funcName)s - %(message)s')

def make_api_request(url):

    try:
        s = requests.Session()
        retries = Retry(total=6, backoff_factor=1, connect=5, status=3, status_forcelist=[400, 401, 404, 500, 501, 502])

        # Prepare the request with Retry
        s.mount("https://", HTTPAdapter(max_retries=retries))
        s = requests.get(url, proxies=def_proxies)

        # Status Code checking
        if s.status_code != 200:
            logging.error("Warning : Status Code call <> 200: {}".format(s.status_code))

        return s.json()

    # Request Exception Handling
    except MaxRetryError as maxer:
        print("Max Retries Error:", maxer)
    except requests.exceptions.HTTPError as errh:
        print("Http Error:", errh)
    except requests.exceptions.ConnectionError as errc:
        print("Error Connecting:", errc)
    except requests.exceptions.Timeout as errt:
        print("Timeout Error:", errt)
    except requests.exceptions.RequestException as err:
        print("OOps: Something Else", err)

在我的日志中没有任何东西显示我已经执行了重试我添加了一个 MaxRetryError 来捕获它,但没有提出任何问题

所以我决定推出 Wireshark :-(

例如,当我使用虚假登录模拟 401 时看到的内容

12  2.917294    192.168.43.85   69.84.209.64    TCP 66  61620 → 443 [SYN] Seq=0 Win=17520 Len=0 MSS=1460 WS=256 SACK_PERM=1
14  2.972088    192.168.43.85   69.84.209.64    TCP 54  61620 → 443 [ACK] Seq=1 Ack=1 Win=17408 Len=0
15  3.168677    192.168.43.85   69.84.209.64    TLSv1.2 571 Client Hello
20  3.314637    192.168.43.85   69.84.209.64    TCP 66  61620 → 443 [ACK] Seq=518 Ack=1337 Win=16128 Len=0 SLE=2673 SRE=4009
22  3.315087    192.168.43.85   69.84.209.64    TCP 54  61620 → 443 [ACK] Seq=518 Ack=4009 Win=17408 Len=0
26  3.316322    192.168.43.85   69.84.209.64    TCP 54  61620 → 443 [ACK] Seq=518 Ack=5619 Win=17408 Len=0
27  3.318149    192.168.43.85   69.84.209.64    TLSv1.2 412 Client Key Exchange, Change Cipher Spec, Encrypted Handshake Message
35  3.475570    192.168.43.85   69.84.209.64    TLSv1.2 363 Application Data
38  3.674813    192.168.43.85   69.84.209.64    TCP 54  61620 → 443 [ACK] Seq=1185 Ack=6179 Win=16896 Len=0
40  3.874986    192.168.43.85   69.84.209.64    TCP 54  61620 → 443 [FIN, ACK] Seq=1185 Ack=6248 Win=16640 Len=0
43  4.034423    192.168.43.85   69.84.209.64    TCP 54  61620 → 443 [ACK] Seq=1186 Ack=6249 Win=16640 Len=0

严格来说,没有执行过重试的证据

标签: pythonapihttprequest

解决方案


发现我的错误我所要做的就是

    res = s.get(url, proxies=def_proxies)

    if res.status_code != 200:
        logging.error("Warning : Status Code call <> 200: {}".format(res.status_code))
    else:
        return res.json()

推荐阅读