Best Practices for Implementing Python Requests Retry: How Do You Handle Failed Requests?

8 Replies, 2344 Views

Hey! For python requests retry, I’ve been using `requests.Session` with a custom retry strategy.

Here’s what I do:
```python
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

retry_strategy = Retry(
total=3,
backoff_factor=1,
status_forcelist=[500, 502, 503, 504]
)

adapter = HTTPAdapter(max_retries=retry_strategy)
session = requests.Session()
session.mount("https://", adapter)
```

This handles most server-side issues, and I add a timeout to avoid hanging forever.

Messages In This Thread



Users browsing this thread: 1 Guest(s)