python 请求重试了哪些 HTTP 响应代码

What HTTP response codes are retried by python Requests

Python 请求默认重试的 HTTP 响应状态代码列表是什么以及重试了多少次。如何更改重试次数?我找不到它的任何文档。

我尝试了下面的代码,401 状态代码重试了两次。

import requests
from http.client import HTTPConnection
from requests.auth import HTTPDigestAuth

HTTPConnection.debuglevel = 1
requests.adapters.DEFAULT_RETRIES = 5

def test():
    data = 'testdata'
    username = 'testuser'
    password = 'test'
    url='https://example.com:443/captionen_0001.vtt'
    try:
        response = requests.put(url, auth=HTTPDigestAuth(username,password), data=data, verify=False)
    except Exception as e:
        print('error'+str(e))

test()
  warnings.warn(
send: b'PUT /channel_captionen_0001.vtt HTTP/1.1\r\nHost: example.com\r\nUser-Agent: python-requests/2.24.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 8\r\n\r\n'
send: b'testdata'
reply: 'HTTP/1.1 401 Authorization Required\r\n'
header: Date: Sat, 05 Feb 2022 07:50:25 GMT
header: WWW-Authenticate: Digest realm="WebDAV", nonce="tE/JnkDX845db3", algorithm=MD5, qop="auth"
  
  warnings.warn(
send: b'PUT /channel_captionen_0001.vtt HTTP/1.1\r\nHost: example.com\r\nUser-Agent: python-requests/2.24.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 8\r\nAuthorization: Digest username="testuser", realm="WebDAV", nonce="tE/JnkDX845db3", uri="/channel_captionen_0001.vtt", response="1c3299c716797e8f36528f6e6dbaeb50", algorithm="MD5", qop="auth", nc=00000001, cnonce="dd0835ef485c6b71"\r\n\r\n'
send: b'testdata'
reply: 'HTTP/1.1 401 Authorization Required\r\n'
header: Date: Sat, 05 Feb 2022 07:50:25 GMT
header: WWW-Authenticate: Digest realm="WebDAV", nonce="/vXKnkDXBQc098a4", algorithm=MD5, qop="auth

不是很明显能找到。你必须知道 requests 不是管理连接的包,urllib3 是。

HTTPAdapter 的源代码中(当你想对 requests 进行更多控制时使用它),max_retries 参数的文档字符串说:

If you need granular control over the conditions under which we retry a request, import urllib3's Retry class and pass that instead

现在Retryclass.

可以参考urllib3的文档

特别阅读status_forcelist参数和RETRY_AFTER_STATUS_CODES(默认:frozenset({413, 429, 503})

更新

import requests
import urllib3

my_code_list = [401, 403, ...]

s = requests.Session()
r = urllib3.util.Retry(status_forcelist=my_code_list)
a = requests.adapters.HTTPAdapter(max_retries=r)
s.mount('http://', a)