Advertisement
kopyl

http requests retrier Python 2

Feb 2nd, 2024
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. import requests
  5.  
  6.  
  7. p = print
  8.  
  9.  
  10. class CurrentDatetime:
  11.  
  12.     date = time.strftime('%d %b %Y')
  13.     time = time.strftime('%H:%M:%S')
  14.  
  15.     def __str__(self):
  16.         return f"{self.date} {self.time}"
  17.  
  18.  
  19. def get_caught_error(additional_message=""):
  20.     exc_type, exc_obj, exc_tb = sys.exc_info()
  21.     fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
  22.     message = (
  23.         f"Now {CurrentDatetime()}\n"
  24.         f"\"{fname}\" caught an error "
  25.         f"{exc_type.__name__} "
  26.         f"on line {exc_tb.tb_lineno}:\n"
  27.         f"\"{exc_obj}\""
  28.         f"\n\n{additional_message}"
  29.     )
  30.     return message
  31.  
  32.  
  33. def define_retry_delay_seconds(
  34.     retry_time_count: int
  35. ) -> int:
  36.  
  37.     retry_in_seconds = 1 * retry_time_count
  38.     if retry_in_seconds > 1800:
  39.         retry_in_seconds = 1800
  40.     return retry_in_seconds
  41.  
  42.  
  43. def retry_request_till_success(
  44.     request: requests.models.Request
  45. ) -> requests.models.Response:
  46.  
  47.     def wrapper(*args, **kwargs):
  48.         retry_time_count = 1
  49.         while True:
  50.             retry_in_seconds = define_retry_delay_seconds(retry_time_count)
  51.             try:
  52.                 response = request(*args, **kwargs)
  53.                 if response == None:  # doc.id#13
  54.                     p("There is no request to retry")
  55.                     return
  56.  
  57.                 if response.status_code in [200, 404]:
  58.                     return response
  59.                 else:
  60.                     p(
  61.                         f"URL: {response.url} "
  62.                         f"Request in {request.__name__} failed. "
  63.                         f"Status_code – {response.status_code} "
  64.                         f"Will retry again in {retry_in_seconds} seconds"
  65.                     )
  66.                     time.sleep(retry_in_seconds)
  67.                     retry_time_count += 1
  68.  
  69.             except (
  70.                 requests.ConnectTimeout,
  71.                 requests.exceptions.ReadTimeout
  72.             ) as e:
  73.                 error = get_caught_error()
  74.                 p(
  75.                     f"Request in {request.__name__} failed. "
  76.                     f"Timed out with error {error}"
  77.                     f"Will retry again in {retry_in_seconds} seconds"
  78.                 )
  79.                 time.sleep(retry_in_seconds)
  80.                 retry_time_count += 1
  81.             except requests.exceptions.ConnectionError as e:
  82.                 sleep_time = 10
  83.                 p(
  84.                     f"Request in {request.__name__} failed @"
  85.                     f"{CurrentDatetime()}. "
  86.                     "No connection to internet... "
  87.                     f"Will retry again in {sleep_time} seconds"
  88.                 )
  89.                 time.sleep(sleep_time)
  90.     return wrapper
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement