繁体   English   中英

Python HTTPS请求:重试API上的状态码返回

[英]Python HTTPS Request : Retry on Status Code return on 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

完全没有重试的证据

冒犯我的错误,我要做的就是

    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()

暂无
暂无

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

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