import hashlib
import time
def hash_response_headers(response):
"""Hashes response headers for retry logic."""
header_string = ""
for key, value in response.headers.items():
header_string += f"{key}:{value}" # Concatenate key:value pairs
hash_object = hashlib.sha256(header_string.encode('utf-8')) # Use SHA256 for hashing
return hash_object.hexdigest() # Return hexadecimal representation
def retry_with_fixed_interval(response, max_retries=3, initial_delay=1):
"""Retries request with fixed intervals based on header hash."""
retries = 0
while retries < max_retries:
try:
# Process the response (e.g., check status code, content)
# Replace this with your actual response processing logic
print(f"Response received (attempt {retries + 1})")
return # Success!
except Exception as e:
print(f"Attempt {retries + 1} failed: {e}")
retries += 1
if retries < max_retries:
delay = initial_delay * (2 ** retries) # Exponential backoff
print(f"Retrying in {delay} seconds...")
time.sleep(delay)
print("Max retries reached. Request failed.")
if __name__ == '__main__':
# Example Usage (replace with your actual response object)
class MockResponse:
def __init__(self, headers, status_code, content=""):
self.headers = headers
self.status_code = status_code
self.content = content
#Simulate a response
headers = {"Content-Type": "application/json", "X-Custom-Header": "value1"}
response = MockResponse(headers, 200)
header_hash = hash_response_headers(response)
print(f"Header Hash: {header_hash}")
retry_with_fixed_interval(response, max_retries=2, initial_delay=1)
Add your comment