def retry_session():
# This will give the total wait time in minutes:
# >>> sum([min((0.3 * (2 ** (i - 1))), 120) / 60 for i in range(24)])
# >>> 30.5575
# This works by the using the minimum time in seconds of the backoff time
# and the max back off time which defaults to 120 seconds. The backoff time
# increases after every failed attempt.
session = requests.Session()
retry = Retry(
total=24,
read=5,
connect=24,
backoff_factor=0.3,
status_forcelist=(500, 502, 504),
method_whitelist=('GET', 'POST'),
)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
return session
评论列表
文章目录