What are the list of HTTP response status codes are retried by default and how many times by the Python Requests. How can I change the number of retries? I couldn't find any documentation for it.
I tried below code and I saw there were two retries done on 401
status code.
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
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/JnkDXBQA=7fb88e30d6a6d59095534a2a6db3bbbe85845db3", 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
It's not obviously to find. You have to know requests
is not the package that manage the connection, urllib3
does.
In the source code of HTTPAdapter
(use it when you want more control on requests
), the docstring on max_retries
parameter said:
If you need granular control over the conditions under which we retry a request, import urllib3's
Retry
class and pass that instead
Now you can refer to the documentation of urllib3
for Retry
class.
Read especially status_forcelist
parameter and RETRY_AFTER_STATUS_CODES
(default: frozenset({413, 429, 503})
)
Update
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.