Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python `响应`with`requests`重试处理_Python_Python Requests_Python Responses - Fatal编程技术网

Python `响应`with`requests`重试处理

Python `响应`with`requests`重试处理,python,python-requests,python-responses,Python,Python Requests,Python Responses,我正在尝试在我的模块中实现/测试请求重试-下面是一个划痕,它具有我试图实现的基本功能 import requests from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry import responses retry_strategy = Retry( total=3, status_forcelist=[429, 500, 502, 50

我正在尝试在我的模块中实现/测试请求重试-下面是一个划痕,它具有我试图实现的基本功能

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import responses

retry_strategy = Retry(
    total=3,
    status_forcelist=[429, 500, 502, 503, 504],
    method_whitelist=["HEAD", "GET", "OPTIONS"]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)

with responses.RequestsMock() as rsps:
    rsps.add(responses.GET,
             "https://en.wikipedia.org/w/api.php",
             status=502)

    response = http.get("https://en.wikipedia.org/w/api.php")
    print(len(rsps.calls))
print(response)
基本设置来自,但在stackoverflow的其他位置使用

我希望len(rsps.calls)为3,因为根据我的理解,这是我们在放弃之前会尝试的重试次数。然而,输出是相反的

1
<Response [502]>
1
这是由于有响应的设置,还是我的初始配置不正确


任何帮助都将不胜感激-谢谢

不允许对响应进行重试。RequestsMock()。这是一个限制。啊,这似乎是这里记录的一个已知问题-感谢您提供的信息。响应不允许重试。RequestsMock()。这是一个限制。啊,这似乎是一个已知的问题记录在这里-谢谢你的信息。