小编Dua*_*cho的帖子

使用请求库的重试历史记录

我正在我的 Orchestrate 脚本中构建一个新的重试功能,我想知道尝试连接到特定 URL 时我的请求方法发生了多少次,以及(如果可能)发生了什么错误。

现在,我需要这个用于记录目的,因为我正在开发一个消息系统,一旦我在一个消息系统中工作,我可能需要这个“重试”信息来了解我何时以及为何在 HTTP 请求中遇到任何类型的问题。微服务环境。

到目前为止,我调试并证明重试按预期工作(我有一个用于我们使用的所有微服务的模拟烧瓶服务器),但我找不到获取“重试历史记录”数据的方法。

换句话说,例如我想看看某个特定的微服务是否只能在第三个请求之后响应,诸如此类的事情。

下面是我现在使用的代码:

from requests import exceptions, Session
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter

def open_request_session():
    # Default retries configs
    toggle = True #loaded from config file

    session = Session()

    if toggle:

        # loaded from config file as 
        parameters = {'total': 5, 
                      'backoff_factor': 0.0, 
                      'http_error_to_retry': [400]}well

        retries = Retry(total=parameters['total'],
                        backoff_factor=parameters['backoff_factor'],
                        status_forcelist=parameters['http_error_to_retry'],
                        # Do not force an exception when False
                        raise_on_status=False)

        session.mount('http://', HTTPAdapter(max_retries=retries))
        session.mount('https://', HTTPAdapter(max_retries=retries))

    return session

# Request
    with open_request_session() …
Run Code Online (Sandbox Code Playgroud)

python python-3.x python-requests

4
推荐指数
1
解决办法
1498
查看次数

标签 统计

python ×1

python-3.x ×1

python-requests ×1