1. import time
  2. import requests
  3. import threading
  4. class RequestThrottler:
  5. def __init__(self, url, delay=1):
  6. self.url = url
  7. self.delay = delay # Delay in seconds
  8. self.lock = threading.Lock() # Thread safety
  9. self.last_request_time = 0
  10. def _throttle(self):
  11. """Internal function to handle throttling."""
  12. with self.lock:
  13. now = time.time()
  14. time_since_last_request = now - self.last_request_time
  15. if time_since_last_request < self.delay:
  16. time.sleep(self.delay - time_since_last_request)
  17. self.last_request_time = time.time()
  18. def make_request(self, params=None):
  19. """Makes a request to the URL with parameter throttling."""
  20. self._throttle() # Ensure throttling delay
  21. try:
  22. response = requests.get(self.url, params=params)
  23. response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
  24. return response
  25. except requests.exceptions.RequestException as e:
  26. print(f"Request failed: {e}")
  27. return None
  28. if __name__ == '__main__':
  29. # Example usage
  30. url = "https://httpbin.org/get" # Replace with your target URL
  31. throtter = RequestThrottler(url, delay=0.5) # Throttle to 0.5 seconds
  32. for i in range(10):
  33. params = {'param1': i}
  34. response = throtter.make_request(params)
  35. if response:
  36. print(f"Request {i} successful. Status code: {response.status_code}")
  37. time.sleep(0.1) # Add a small delay between requests for demonstration

Add your comment