Retry with python requests

Source: https://stackoverflow.com/a/35504626

It will retry at intervals of the number of seconds specified by backoff_factor x the number of retries. In the following cases, the first retry is 1 second and the second retry is 2 seconds. Retry when the status code specified in status_forcelist is returned or when it times out. The parameters that can be specified for Retry are described in here.

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

s = requests.Session()

retries = Retry(total=5,
                backoff_factor=1,
                status_forcelist=[ 500, 502, 503, 504 ])

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


r = s.request('GET', 'http://localhost:5000', timeout=2, headers={'Authorization': 'foobar'})
r.raise_for_status()

Postscript

If you use a library called retry, you can go! I thought, but with this library, it seems that it is not possible to sort the processing by status code.

Recommended Posts

Retry with python requests
Retry post request using python requests
Get the weather with Python requests
Get the weather with Python requests 2
Send multipart / form-data with python requests
Statistics with python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Call APIGateWay with APIKey in python requests
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Serial communication with python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
python decorator to retry
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Primality test with python
Run Blender with python
Solve Sudoku with Python
Python starting with Windows 7
Heatmap with Python + matplotlib
Multi-process asynchronously with python