-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout.py
executable file
·44 lines (35 loc) · 1.09 KB
/
timeout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# https://majornetwork.net/2022/04/handling-retries-in-python-requests/
import time
import requests
import urllib3
from requests.adapters import HTTPAdapter
from urllib3 import Retry
def timeout():
print(urllib3.__version__)
session = requests.Session()
# backoff factor of 1 for the retry delay (the formula for the delay is
# {backoff factor} * (2 ** ({retry number} - 1)), except that the first
# retry is always immediate)
adapter = HTTPAdapter(
max_retries=Retry(
total=5,
backoff_factor=1,
allowed_methods=None,
status_forcelist=[429, 500, 502, 503, 504],
)
)
session.mount("http://", adapter)
session.mount("https://", adapter)
start_time = time.monotonic()
try:
r = session.get("http://httpbin.org/status/503")
# Do something useful with r.
print(r)
except Exception as e:
print(type(e))
print(e)
stop_time = time.monotonic()
print(round(stop_time - start_time, 2), "seconds")
if __name__ == "__main__":
timeout()