{"id":62451,"date":"2024-12-27T12:21:41","date_gmt":"2024-12-27T12:21:41","guid":{"rendered":"https:\/\/proxidize.com\/?post_type=blog&#038;p=62451"},"modified":"2025-11-27T13:50:13","modified_gmt":"2025-11-27T13:50:13","slug":"python-requests-retry","status":"publish","type":"blog","link":"https:\/\/proxidize.com\/blog\/python-requests-retry\/","title":{"rendered":"How to Handle a Python Requests Retry"},"content":{"rendered":"\n<p>Python\u2019s requests library should simplify making HTTP requests but occasionally, it could fail to retrieve the needed data because of an error. This article will explore the most common errors that come up with Python requests and provide some advice on how to create Python requests retry mechanisms. Two of the most common ways are through using Sessions and HTTPAdapter or by using a retry logic wrapper. We will present both options along with some handy code snippets to help you with your tasks.&nbsp;<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized centered\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1010\" height=\"569\" src=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/core-concepts-of-retrying-python-request.jpg\" alt=\"Image of a man sitting on a computer while browsers surround him and a message reading ERROR. Text above the image reads &quot;Core Concepts of Retrying Python Request&quot;\" class=\"wp-image-62438\" style=\"object-fit:cover\" srcset=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/core-concepts-of-retrying-python-request.jpg 1010w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/core-concepts-of-retrying-python-request-300x169.jpg 300w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/core-concepts-of-retrying-python-request-768x433.jpg 768w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/core-concepts-of-retrying-python-request-600x338.jpg 600w\" sizes=\"(max-width: 1010px) 100vw, 1010px\" \/><\/figure>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Core Concepts of Retrying Python Request<\/h2>\n\n\n\n<p>The Python requests retry mechanism is a technique where you tell your response code to resend a request automatically if an HTTP error happens. The retry logic is usually decision-based and depends on why and when the failure occurs.&nbsp;<\/p>\n\n\n\n<p>Not all Python request errors should trigger a retry. It is important to know the cause of the failure and decide if a retry is even necessary. It is better to apply retries in specific scenarios such as <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/postgresql\/flexible-server\/concepts-connectivity#:~:text=A%20transient%20error%2C%20also%20known,hardware%20or%20network%20failure%20happens.\" target=\"_blank\" rel=\"noopener\">transient server problems<\/a>. However, retrying after client-side or permanent issues is typically not needed. You can retry a Python request immediately after a failure occurs but that strategy could overload the server and initiate an <a href=\"https:\/\/proxidize.com\/blog\/bypass-ip-ban\/\">IP ban<\/a>.<\/p>\n\n\n\n<p>Instead of doing this instantaneously, you should implement a delay between retries. However, setting a delay could create a bot-like pattern and make it easier for a website to detect and ban you. It is recommended to use an exponential backoff strategy to avoid this or to implement a rotating <a href=\"https:\/\/proxidize.com\/proxy-server\/mobile-proxy\/\">mobile proxy<\/a> so that your IP address is constantly changing, thus diminishing the chances of detection.<\/p>\n\n\n\t\t<div data-elementor-type=\"container\" data-elementor-id=\"85916\" class=\"elementor elementor-85916\" data-elementor-post-type=\"elementor_library\">\n\t\t\t\t<div class=\"elementor-element elementor-element-6227acb e-con-full no-scale elementor-hidden-mobile_extra elementor-hidden-mobile e-flex e-con e-child\" data-id=\"6227acb\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;background_background&quot;:&quot;gradient&quot;}\">\n\t\t<div class=\"elementor-element elementor-element-08fce92 e-grid e-con-full e-con e-child\" data-id=\"08fce92\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t<div class=\"elementor-element elementor-element-426c265 e-con-full e-flex e-con e-child\" data-id=\"426c265\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-b474c5f elementor-widget elementor-widget-heading\" data-id=\"b474c5f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<p class=\"elementor-heading-title elementor-size-default\">IP rotation, city and carrier targeting,<br>\nsticky sessions \u2014 control it all via API<\/p>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-e86f5a3 e-con-full e-flex e-con e-child\" data-id=\"e86f5a3\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t<div class=\"elementor-element elementor-element-6e3bca6 e-con-full e-flex e-con e-child\" data-id=\"6e3bca6\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-2c6c495 elementor-widget__width-initial elementor-widget elementor-widget-image\" data-id=\"2c6c495\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"125\" height=\"80\" src=\"https:\/\/proxidize.com\/wp-content\/uploads\/2025\/10\/20-2.svg\" class=\"attachment-full size-full wp-image-86191\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-17d8cea inline-CTA elementor-widget elementor-widget-button\" data-id=\"17d8cea\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"button.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<div class=\"elementor-button-wrapper\">\n\t\t\t\t\t<a class=\"elementor-button elementor-button-link elementor-size-sm\" href=\"https:\/\/proxidize.com\/mobile-proxy-pricing\/?coupon_code=20OFFMPB\" target=\"_blank\">\n\t\t\t\t\t\t<span class=\"elementor-button-content-wrapper\">\n\t\t\t\t\t\t\t\t\t<span class=\"elementor-button-text\">Buy Proxies Now<\/span>\n\t\t\t\t\t<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\n\n\n\n<div style=\"height:12px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Types of Failed Python Request<\/h3>\n\n\n\n<p>Understanding why your Python request failed will allow you to develop mitigation strategies for each case. There are two main categories of failed requests including requests that timed out and requests that returned an HTTP error. When the request times out, the client did not receive a reply within a specific time frame. This could happen because of a server overload, server response issues, or due to slow network connections. If you face a timeout response, check your internet connection. If the connection is stable, the issue could be server-related. For requests that return an HTTP error, the server could be active but the request cannot be processed successfully. This failure will come with a specific status code and error message that tells you what went wrong. Here are some of the most common errors:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/proxidize.com\/blog\/403-error\/\" target=\"_blank\" rel=\"noreferrer noopener\">403 Forbidden<\/a><\/strong>: The server understood the request but did not respond appropriately because you are not allowed to access the document or even the entire server.\u00a0<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/proxidize.com\/blog\/error-code-429\/\" target=\"_blank\" rel=\"noreferrer noopener\">429 Too Many Requests<\/a><\/strong>: This is one of the most frequent HTTP errors when <a href=\"https:\/\/proxidize.com\/use-cases\/web-scraping\/\" target=\"_blank\" rel=\"noreferrer noopener\">web scraping<\/a>. It comes up if you are sending too many requests to the same endpoint. This can be solved by switching your proxy or trying to retry a failed request.\u00a0<\/li>\n\n\n\n<li><strong>500 Internal Server Error<\/strong>: This error code comes up when something has failed on the server\u2019s end. Trying again in a few minutes should solve this issue.&nbsp;<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/proxidize.com\/blog\/error-code-502\/\" target=\"_blank\" rel=\"noreferrer noopener\">502 Bad Gateway<\/a><\/strong>: Similar to the 500 error, the 502 error means something went wrong with the upstream server. Trying again shortly should get rid of the issue.\u00a0<\/li>\n\n\n\n<li><strong>503 Service Unavailable<\/strong>: The server is either completely down or unavailable. This is an issue that will be solved by the website administrator.&nbsp;<\/li>\n\n\n\n<li><strong><a href=\"https:\/\/proxidize.com\/blog\/error-code-504\/\" target=\"_blank\" rel=\"noreferrer noopener\">504 Gateway Timeout<\/a><\/strong>: This error indicates networking issues that could be caused by either end. Retrying with increased delays should fix the issue.\u00a0<\/li>\n<\/ul>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized centered\"><img decoding=\"async\" width=\"1010\" height=\"569\" src=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/python-request-retry-strategies.jpg\" alt=\"Image of a man standing in front of a road closure with a large browser page showing a 404 error. Text above the image reads &quot;Python Request Retry Strategies&quot;\" class=\"wp-image-62440\" style=\"object-fit:cover\" srcset=\"https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/python-request-retry-strategies.jpg 1010w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/python-request-retry-strategies-300x169.jpg 300w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/python-request-retry-strategies-768x433.jpg 768w, https:\/\/proxidize.com\/wp-content\/uploads\/2024\/12\/python-request-retry-strategies-600x338.jpg 600w\" sizes=\"(max-width: 1010px) 100vw, 1010px\" \/><\/figure>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Python Request Retry Strategies<\/h2>\n\n\n\n<p>If you find yourself in a position where you do need to implement a Python request retry, there are two options available to you. You can use one of Python\u2019s existing retry wrappers or you could build your own. Using the pre-existing wrapper, <a href=\"https:\/\/docs.nestjs.com\/faq\/http-adapter\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">HTTPAdapter<\/a> will let you specify a retry strategy and change the request behavior while building your own, allowing you to implement custom error handlers, logs, and more.<\/p>\n\n\n\n<div style=\"height:12px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Built-in Python Request Retry Mechanism&nbsp;<\/h3>\n\n\n\n<p>Python request uses the <a href=\"https:\/\/github.com\/urllib3\/urllib3\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">urllib3 HTTP client<\/a> directly. You can set up retries in Python with requests\u2019 HTTP adapter retry class and the Retry utility class from the urllib3 package. The HTTPAdapter class lets you specify a retry strategy and change the request behavior. To use a simple Python Request retry strategy with HTTPAdapter, import the required libraries and define your options. As an example, we will set the maximum number of request to 4 and retry attempts if the error has a status code of 403, 429, 500, and 502.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2\"><span role=\"button\" data-code=\"# pip3 install requests\nimport requests\nfrom requests.adapters import HTTPAdapter\nfrom urllib3.util import Retry\n# define the retry strategy\nretry_strategy = Retry(\n    total=4,  # maximum number of retries\n    status_forcelist=[403, 429, 500, 502],  # the HTTP status codes to retry on\n)\" style=\"color:#232136;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\"><code><span class=\"line\"><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> pip3 install requests<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">import<\/span><span style=\"color: #E0DEF4\"> requests<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">from<\/span><span style=\"color: #E0DEF4\"> requests<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">adapters <\/span><span style=\"color: #3E8FB0\">import<\/span><span style=\"color: #E0DEF4\"> HTTPAdapter<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">from<\/span><span style=\"color: #E0DEF4\"> urllib3<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">util <\/span><span style=\"color: #3E8FB0\">import<\/span><span style=\"color: #E0DEF4\"> Retry<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> define the retry strategy<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">retry_strategy <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> Retry<\/span><span style=\"color: #908CAA\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">total<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #EA9A97\">4<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> maximum number of retries<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">status_forcelist<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">[<\/span><span style=\"color: #EA9A97\">403<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">429<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">500<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">502<\/span><span style=\"color: #908CAA\">],<\/span><span style=\"color: #E0DEF4\">  <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> the HTTP status codes to retry on<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>After installing the necessary libraries and defining the strategy, pass it to the HTTPAdapter in a new adapter object and then mount the adapter to a session retry object to use for all requests.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2\"><span role=\"button\" data-code=\"adapter = HTTPAdapter(max_retries=retry_strategy)\n# create a new session object\nsession = requests.Session()\nsession.mount(&quot;http:\/\/&quot;, adapter)\nsession.mount(&quot;https:\/\/&quot;, adapter)\n# make a request using the session object\nresponse = session.get(&quot;www.websiteexample.com&quot;)\nif response.status_code == 200:\n    print(f&quot;SUCCESS: {response.text}&quot;)\nelse:\n    print(f&quot;FAILED with status {response.status_code}&quot;)\" style=\"color:#232136;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\"><code><span class=\"line\"><span style=\"color: #E0DEF4\">adapter <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> HTTPAdapter<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #C4A7E7;font-style: italic\">max_retries<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\">retry_strategy<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> create a new session object<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">session <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> requests<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">Session<\/span><span style=\"color: #908CAA\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">session<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">mount<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #F6C177\">&quot;http:\/\/&quot;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> adapter<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">session<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">mount<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #F6C177\">&quot;https:\/\/&quot;<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> adapter<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> make a request using the session object<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> session<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">get<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #F6C177\">&quot;www.websiteexample.com&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">if<\/span><span style=\"color: #E0DEF4\"> response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">status_code <\/span><span style=\"color: #3E8FB0\">==<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">200<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #EB6F92;font-style: italic\">print<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #3E8FB0\">f<\/span><span style=\"color: #F6C177\">&quot;SUCCESS: <\/span><span style=\"color: #3E8FB0\">{<\/span><span style=\"color: #E0DEF4\">response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">text<\/span><span style=\"color: #3E8FB0\">}<\/span><span style=\"color: #F6C177\">&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">else<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #EB6F92;font-style: italic\">print<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #3E8FB0\">f<\/span><span style=\"color: #F6C177\">&quot;FAILED with status <\/span><span style=\"color: #3E8FB0\">{<\/span><span style=\"color: #E0DEF4\">response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">status_code<\/span><span style=\"color: #3E8FB0\">}<\/span><span style=\"color: #F6C177\">&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:12px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Custom Retry Strategies<\/h3>\n\n\n\n<p>While using the available built-in libraries is always an option, you might want to try building your own customer wrapper for retry login. Doing so gives you the ability to implement more custom options. Let us create a Python function that will simulate the retry logic implementation of the first method. The function will accept the target URL as its first argument and then the maximum retries and status_forcelist to specify the type of errors to retry the request.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2\"><span role=\"button\" data-code=\"import requests\ndef retry_request(\n    url,\n    total=4,\n    status_forcelist=[\n        403,\n        429,\n        500,\n        502,\n    ],\n    **kwargs,\n):\n    # store the last response in an empty variable\n    last_response = None\n    # implement retry\n    for _ in range(total):\n        try:\n            response = requests.get(url, **kwargs)\n            if response.status_code in status_forcelist:\n                # track the last response\n                last_response = response\n                # retry request\n                continue\n            else:\n                return response\n        except requests.exceptions.ConnectionError:\n            pass\n    # log the response after the retry\n    return last_response\nresponse = retry_request(&quot;www.websiteexample.com&quot;)\nif response.status_code == 200:\n    print(f&quot;SUCCESS: {response.text}&quot;)\nelse:\n    print(f&quot;FAILED with status {response.status_code}&quot;)\" style=\"color:#232136;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">import<\/span><span style=\"color: #E0DEF4\"> requests<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">def<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">retry_request<\/span><span style=\"color: #908CAA\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">url<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">total<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #EA9A97\">4<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">status_forcelist<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">[<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #EA9A97\">403<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #EA9A97\">429<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #EA9A97\">500<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #EA9A97\">502<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA\">],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">**<\/span><span style=\"color: #C4A7E7;font-style: italic\">kwargs<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> store the last response in an empty variable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    last_response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">None<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> implement retry<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">for<\/span><span style=\"color: #E0DEF4\"> _ <\/span><span style=\"color: #3E8FB0\">in<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EB6F92;font-style: italic\">range<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #E0DEF4\">total<\/span><span style=\"color: #908CAA\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">            response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> requests<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">get<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #E0DEF4\">url<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">**<\/span><span style=\"color: #E0DEF4\">kwargs<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">            <\/span><span style=\"color: #3E8FB0\">if<\/span><span style=\"color: #E0DEF4\"> response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">status_code <\/span><span style=\"color: #3E8FB0\">in<\/span><span style=\"color: #E0DEF4\"> status_forcelist<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> track the last response<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                last_response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> response<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> retry request<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #3E8FB0\">continue<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">            <\/span><span style=\"color: #3E8FB0\">else<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> response<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #3E8FB0\">except<\/span><span style=\"color: #E0DEF4\"> requests<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">exceptions<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">ConnectionError<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">            <\/span><span style=\"color: #3E8FB0\">pass<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> log the response after the retry<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> last_response<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> retry_request<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #F6C177\">&quot;www.websiteexample.com&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">if<\/span><span style=\"color: #E0DEF4\"> response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">status_code <\/span><span style=\"color: #3E8FB0\">==<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">200<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #EB6F92;font-style: italic\">print<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #3E8FB0\">f<\/span><span style=\"color: #F6C177\">&quot;SUCCESS: <\/span><span style=\"color: #3E8FB0\">{<\/span><span style=\"color: #E0DEF4\">response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">text<\/span><span style=\"color: #3E8FB0\">}<\/span><span style=\"color: #F6C177\">&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">else<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #EB6F92;font-style: italic\">print<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #3E8FB0\">f<\/span><span style=\"color: #F6C177\">&quot;FAILED with status <\/span><span style=\"color: #3E8FB0\">{<\/span><span style=\"color: #E0DEF4\">response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">status_code<\/span><span style=\"color: #3E8FB0\">}<\/span><span style=\"color: #F6C177\">&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>This script can be slightly improved upon with an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Exponential_backoff\" target=\"_blank\" rel=\"noreferrer noopener\">exponential backoff<\/a>. To retry a Python request with a custom backoff, take the code we provided as a base and then create a separate function named <code>backoff_delay<\/code> to calculate the delay.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2\"><span role=\"button\" data-code=\"def backoff_delay(backoff_factor, attempts):\n    # backoff algorithm\n    delay = backoff_factor * (2 ** (attempts - 1))\n    return delay\" style=\"color:#232136;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">def<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">backoff_delay<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #C4A7E7;font-style: italic\">backoff_factor<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #C4A7E7;font-style: italic\">attempts<\/span><span style=\"color: #908CAA\">):<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> backoff algorithm<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    delay <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> backoff_factor <\/span><span style=\"color: #3E8FB0\">*<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #EA9A97\">2<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">**<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #E0DEF4\">attempts <\/span><span style=\"color: #3E8FB0\">-<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">1<\/span><span style=\"color: #908CAA\">))<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> delay<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>After you have done that, add the backoff factor and use the exponential delay with the time module:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e0def4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1.25rem;--cbp-tab-width:2\"><span role=\"button\" data-code=\"def retry_request(\n    url,\n    backoff_factor=2,\n    total=4,\n    status_forcelist=[\n        403,\n        429,\n        500,\n        502,\n    ],\n    **kwargs,\n):\n    # store the last response in an empty variable\n    last_response = None\n    # implement retry\n    for attempt in range(total):\n        try:\n            response = requests.get(url, **kwargs)\n            if response.status_code in status_forcelist:\n                # implement backoff\n                delay = backoff_delay(backoff_factor, attempt)\n                sleep(delay)\n                print(f&quot;retrying in {delay} seconds&quot;)\n                # track the last response\n                last_response = response\n                # retry request\n                continue\n            else:\n                return response\n        except requests.exceptions.ConnectionError:\n            pass\n    # log the response after the retry\n    return last_response\nresponse = retry_request(&quot;https:\/\/www.scrapingcourse.com\/ecommerce&quot;)\nif response.status_code == 200:\n    print(f&quot;SUCCESS: {response.text}&quot;)\nelse:\n    print(f&quot;FAILED with status {response.status_code}&quot;)\" style=\"color:#232136;background-color:#e0def4\" aria-label=\"Copy\" data-copied-text=\"Copied!\" data-has-text-button=\"textSimple\" data-inside-header-type=\"none\" aria-live=\"polite\" class=\"code-block-pro-copy-button\"><span class=\"cbp-btn-text\">Copy<\/span><\/span><pre class=\"shiki rose-pine-moon\" style=\"background-color: #232136\"><code><span class=\"line\"><span style=\"color: #3E8FB0\">def<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">retry_request<\/span><span style=\"color: #908CAA\">(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">url<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">backoff_factor<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #EA9A97\">2<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">total<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #EA9A97\">4<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #C4A7E7;font-style: italic\">status_forcelist<\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #908CAA\">[<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #EA9A97\">403<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #EA9A97\">429<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #EA9A97\">500<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #EA9A97\">502<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA\">],<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">**<\/span><span style=\"color: #C4A7E7;font-style: italic\">kwargs<\/span><span style=\"color: #908CAA\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #908CAA\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> store the last response in an empty variable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    last_response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">None<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> implement retry<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">for<\/span><span style=\"color: #E0DEF4\"> attempt <\/span><span style=\"color: #3E8FB0\">in<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EB6F92;font-style: italic\">range<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #E0DEF4\">total<\/span><span style=\"color: #908CAA\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #3E8FB0\">try<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">            response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> requests<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">get<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #E0DEF4\">url<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #3E8FB0\">**<\/span><span style=\"color: #E0DEF4\">kwargs<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">            <\/span><span style=\"color: #3E8FB0\">if<\/span><span style=\"color: #E0DEF4\"> response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">status_code <\/span><span style=\"color: #3E8FB0\">in<\/span><span style=\"color: #E0DEF4\"> status_forcelist<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> implement backoff<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                delay <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> backoff_delay<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #E0DEF4\">backoff_factor<\/span><span style=\"color: #908CAA\">,<\/span><span style=\"color: #E0DEF4\"> attempt<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                sleep<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #E0DEF4\">delay<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #EB6F92;font-style: italic\">print<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #3E8FB0\">f<\/span><span style=\"color: #F6C177\">&quot;retrying in <\/span><span style=\"color: #3E8FB0\">{<\/span><span style=\"color: #E0DEF4\">delay<\/span><span style=\"color: #3E8FB0\">}<\/span><span style=\"color: #F6C177\"> seconds&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> track the last response<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                last_response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> response<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> retry request<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #3E8FB0\">continue<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">            <\/span><span style=\"color: #3E8FB0\">else<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">                <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> response<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">        <\/span><span style=\"color: #3E8FB0\">except<\/span><span style=\"color: #E0DEF4\"> requests<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">exceptions<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">ConnectionError<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">            <\/span><span style=\"color: #3E8FB0\">pass<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #908CAA;font-style: italic\">#<\/span><span style=\"color: #6E6A86;font-style: italic\"> log the response after the retry<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #3E8FB0\">return<\/span><span style=\"color: #E0DEF4\"> last_response<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">response <\/span><span style=\"color: #3E8FB0\">=<\/span><span style=\"color: #E0DEF4\"> retry_request<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #F6C177\">&quot;https:\/\/www.scrapingcourse.com\/ecommerce&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">if<\/span><span style=\"color: #E0DEF4\"> response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">status_code <\/span><span style=\"color: #3E8FB0\">==<\/span><span style=\"color: #E0DEF4\"> <\/span><span style=\"color: #EA9A97\">200<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #EB6F92;font-style: italic\">print<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #3E8FB0\">f<\/span><span style=\"color: #F6C177\">&quot;SUCCESS: <\/span><span style=\"color: #3E8FB0\">{<\/span><span style=\"color: #E0DEF4\">response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">text<\/span><span style=\"color: #3E8FB0\">}<\/span><span style=\"color: #F6C177\">&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #3E8FB0\">else<\/span><span style=\"color: #908CAA\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E0DEF4\">    <\/span><span style=\"color: #EB6F92;font-style: italic\">print<\/span><span style=\"color: #908CAA\">(<\/span><span style=\"color: #3E8FB0\">f<\/span><span style=\"color: #F6C177\">&quot;FAILED with status <\/span><span style=\"color: #3E8FB0\">{<\/span><span style=\"color: #E0DEF4\">response<\/span><span style=\"color: #908CAA\">.<\/span><span style=\"color: #E0DEF4\">status_code<\/span><span style=\"color: #3E8FB0\">}<\/span><span style=\"color: #F6C177\">&quot;<\/span><span style=\"color: #908CAA\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Handling Python request retries properly ensures robust and error-tolerant applications. By using the strategies we had discussed such as exponential backoff, managing HTTP error handling, and using proxies when necessary, you can enhance the reliability of your code.&nbsp;<\/p>\n\n\n\n<p>Key Takeaways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use exponential backoff to implement delays between retries. This will avoid a server overload and reduce the risk of detection.&nbsp;<\/li>\n\n\n\n<li>Prioritize retry-appropriate errors such as 429 and 500-504 while avoiding any unnecessary retries for client-side errors like 403.&nbsp;<\/li>\n\n\n\n<li>Use proxies to enhance scalability. Rotating proxies can help you manage retries when scraping and will assist with avoiding IP bans caused by rate limits.&nbsp;<\/li>\n\n\n\n<li>Utilize built-in tools like HTTPAdapter and Retry from urllib3 for easier and more reliable retry strategies. Building your own custom retry logic is a great choice but only necessary for more advanced error handling or for tailored delay mechanisms.&nbsp;<\/li>\n<\/ul>\n\n\n\n<div style=\"height:12px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If you decide to use Python request built-in libraries or build your own solution, optimizing retry mechanisms allows you to tackle transient errors, handle rate limits, and maintain efficient interactions with servers.&nbsp;<\/p>\n","protected":false},"author":2627,"featured_media":75565,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","categories":[110],"tags":[],"class_list":["post-62451","blog","type-blog","status-publish","format-standard","has-post-thumbnail","hentry","category-web-scraping-and-automation"],"acf":[],"_links":{"self":[{"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/blog\/62451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/users\/2627"}],"replies":[{"embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/comments?post=62451"}],"version-history":[{"count":7,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/blog\/62451\/revisions"}],"predecessor-version":[{"id":90512,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/blog\/62451\/revisions\/90512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/media\/75565"}],"wp:attachment":[{"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/media?parent=62451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/categories?post=62451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/proxidize.com\/wp-json\/wp\/v2\/tags?post=62451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}