Implementing Retry Mechanism in Python urllib3 for Resilient HTTP Requests
Write a Python program that sets up a retry mechanism for failed requests with urllib3.Retry class.
Sample Solution:
Python Code :
Sample Output:
Request Successful: <!doctype html< <html< <head< <title<Example Domain</title< <meta charset="utf-8" /< <meta http-equiv="Content-type" content="text/html; charset=utf-8" /< <meta name="viewport" content="width=device-width, initial-scale=1" /< <style type="text/css"< body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style< </head< <body< <div< <h1<Example Domain</h1< <p<This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p< <p<<a href="https://www.iana.org/domains/example"<More information...</a<</p< </div< </body< </html< runfile('C:/Users/ME/untitled30.py', wdir='C:/Users/ME') Request Successful: <!doctype html< <html< <head< <title<Example Domain</title< <meta charset="utf-8" /< <meta http-equiv="Content-type" content="text/html; charset=utf-8" /< <meta name="viewport" content="width=device-width, initial-scale=1" /< <style type="text/css"< body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style< </head< <body< <div< <h1<Example Domain</h1< <p<This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p< <p<<a href="https://www.iana.org/domains/example"<More information...</a<</p< </div< </body< </html<
Explanation:
Here's a brief explanation of the above Python urllib3 library code:
- Import Libraries: Import the necessary libraries, including "urllib3" for making HTTP requests.
- Define Function: Create a function "make_request_with_retry()" that takes a URL as a parameter and makes a request with a retry mechanism.
- Configure Retry Class: Create a "Retry" class instance with parameters like 'total' (total number of retries), 'backoff_factor' (factor by which retry delays increase), and 'status_forcelist' (HTTP status codes to retry).
- Create PoolManager: Create an instance of "urllib3.PoolManager" with the configured 'Retry' class.
- Make Request with Retry: Use the "http.request" method to make a GET request with the retry mechanism.
- Handle Response: Check if the request was successful (status code 200) and print the response. Otherwise, print an error message.
- Exception Handling: Catch and handle 'MaxRetryError' exceptions that might occur when the maximum number of retries is exceeded.
- Make Request with Retry: Call the "make_request_with_retry()" function to initiate the request with the configured retry mechanism.
Flowchart:

Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Implementing a Custom SSL Context for Secure HTTPS requests in Python urllib3.
Next: Python file upload with urllib3: Making POST Requests simplified.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.