Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: Jean-Aimé Wehrung
Added: Jun 7, 2021 9:35 AM
Views: 196
Tags: timeout
  1. import requests
  2. from requests.adapters import TimeoutSauce
  3.  
  4. REQUESTS_TIMEOUT_SECONDS = 60
  5.  
  6. class CustomTimeout(TimeoutSauce):
  7.     def __init__(self, *args, **kwargs):
  8.         if kwargs["connect"] is None:
  9.             kwargs["connect"] = REQUESTS_TIMEOUT_SECONDS
  10.         if kwargs["read"] is None:
  11.             kwargs["read"] = REQUESTS_TIMEOUT_SECONDS
  12.         super().__init__(*args, **kwargs)
  13.  
  14.  
  15. # Set it globally, instead of specifying ``timeout=..`` kwarg on each call.
  16. requests.adapters.TimeoutSauce = CustomTimeout
  17.  
  18. REQUESTS_MAX_RETRIES = 15
  19. session = requests.Session()
  20. adapter = requests.adapters.HTTPAdapter(max_retries=REQUESTS_MAX_RETRIES)
  21. session.mount('https://', adapter)