I want to retry the python requests.get(). I know we can do it with sessions but in a specific case I need to retry the requests.get() only so I wanted to know how do that? Is there any parameter like in session that we can pass?
Asked
Active
Viewed 4,339 times
0
-
https://stackoverflow.com/questions/15431044/can-i-set-max-retries-for-requests-request – Rahul Jan 06 '22 at 05:28
-
I can not find any solution without using session? – Captain Jan 06 '22 at 06:12
-
See in answer.. – Rahul Jan 06 '22 at 10:09
1 Answers
2
you can use backoff library, I'm using it and I think that it's a cleaner solution than adapters and sessions
A basic example:
import backoff
@backoff.on_exception(
backoff.expo,
requests.exceptions.RequestException,
max_tries=5,
giveup=lambda e: e.response is not None and e.response.status_code < 500
)
def publish(self, data):
r = requests.post(url, timeout=10, json=data)
r.raise_for_status()

Alex Shani
- 141
- 5