Retry post request using python requests

I've been addicted to it, so I made a memo for the memorial service.

When using the requests module of python, if you want to implement retry processing, you can use Retry of urllib3.util.

When status code 500 came back in post this time, I wanted to wait for a certain period of time and then retry, so I used Retry, but even if 500 is returned, it does not retry ... In conclusion, if you want to retry in post, you have to explicitly allow the POST request by passing the allowed_methos argument.

test.py



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

logging.basicConfig(level=logging.DEBUG)

session = requests.Session()

retries = Retry(total=5,backoff_factor=1,allowed_methods=['POST'],status_forcelist = [500])
retries_default = Retry(total=5,backoff_factor=1,status_forcelist = [500])
logging.debug(repr(retries.__dict__))
logging.debug(repr(retries_default.__dict__)) #If not specified, allowed_methos has no POST
session.mount('https://',HTTPAdapter(max_retries=retries))

session.post('https://httpbin.org/status/500')

Recommended Posts

Retry post request using python requests
Retry with python requests
Save images using python3 requests
Post to Twitter using Python
[Python] POST wav files with requests [POST]
Log in to Slack using requests in Python
Start using Python
Scraping using Python
Operate Redmine using Python Redmine
Download images using requests
Http request in python
Fibonacci sequence using Python
Data analysis using Python 0
python decorator to retry
Data cleaning using Python
Using Python #external packages
Register a ticket with redmine API using python requests
Write data to KINTONE using the Python requests module
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
Name identification using python
Python Requests status code
Notes using Python subprocesses
Try using Tweepy [Python2.7]
Disable SSL validation without using verify = False in Python requests
Python notes using perl-ternary operator
Flatten using Python yield from
Scraping using Python 3.5 async / await
Post from Python to Slack
Post to vim → Python → Slack
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
POST json with Python3 script
Using Quaternion with Python ~ numpy-quaternion ~
Aim python library master (18) requests
Try using Kubernetes Client -Python-
Post to slack with Python 3
Python notes using perl-special variables
[Python] Using OpenCV with Python (Basic)
Scraping using Python 3.5 Async syntax
Website change monitoring using python
Start to Selenium using python
Search algorithm using word2vec [python]
Change python version using pyenv
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
Create JIRA tickets using Python
Instrument control using Python [pyvisa]
Manipulate spreadsheets locally using Python
Python memo using perl --join
Web scraping using Selenium (Python)
[Python] I tried using OpenPose
Post processing of python (NG)
[Python] JSON validation using Voluptuous
python beautifulsoup requests glob find_all
Broadcast on LINE using python
Data analysis using python pandas
Translate using googletrans in Python
Using Python mode in Processing
Using OpenCV with Python @Mac