Sling Academy
Home/Python/Python ‘requests’ module: TooManyRedirects Exception [Solved]

Python ‘requests’ module: TooManyRedirects Exception [Solved]

Last updated: January 02, 2024

While developing in Python, encountering exceptions is a common part of the debugging process. One such exception is TooManyRedirects, which can occur when using the requests module to make HTTP requests. This exception indicates that a request exceeded the maximum number of allowed redirections. To understand and resolve this, it’s essential first to comprehend the reasons that trigger the error and then explore the different solutions.

Understanding the Cause

The TooManyRedirects exception happens when the requests module follows a chain of redirects that exceeds its set limit. A redirect loop or an overly-long redirect chain typically causes it. The default maximum limit is set to 30, but this can be configured. Here’s a brief overview of possible causes:

  • A redirect loop where the server instructs the client to keep making new requests that never resolve to a final destination.
  • An excessively long chain of valid redirects, which surpasses the allowances defined in the requests configurations.
  • Incorrect server configurations or URL structures that lead to unresolvable redirections.

Solution 1: Increase Max Redirects Limit

If you believe that the redirect chain is valid and just exceeds the default limit, you can increase the maximum number of redirections allowed. Be cautious with this approach as it might mask an underlying issue.

Steps to implement:

  1. Import the requests module in your Python script.
  2. Set the limit using the max_redirects parameter within the requests.get() or similar methods.
  3. Make our HTTP request with the new limit.

Code example:

import requests

try:
    response = requests.get('http://example.com', max_redirects=50)
    print(response.content)
except requests.TooManyRedirects as e:
    print(e)

Advantages: Relatively straightforward, can resolve the issue if it’s simply a matter of an extensive but expected redirect chain.

Limitations: Might not fix the problem if the redirects are actually part of a loop; you risk getting stuck in a substantial but endless redirection.

Solution 2: Set Allow Redirects to False

To avoid getting caught in redirection loops, you can tell the requests module not to follow redirects at all. This is useful for inspecting the response and diagnosing the issue.

Steps to implement:

  1. Import the requests module.
  2. Make the HTTP request with parameter allow_redirects=False.
  3. Analyze the response HTTP status code and the ‘Location’ header to diagnose the issue.

Code example:

import requests

response = requests.get('http://example.com', allow_redirects=False)
if response.status_code in (301, 302):
    new_location = response.headers['Location']
    print(f'Redirect to {new_location}')
else:
    print(response.content)

Advantages: Prevents the TooManyRedirects exception by cutting down the redirection process early.
Limitations: This approach won’t actually ‘follow’ any redirects, so it might require additional management for handling subsequent requests.

Solution 3: Utilize a Retry Strategy with Backoff

Implementing a custom retry strategy can help to overcome redirection issues. This may involve using a backoff algorithm to avoid hitting the redirect limit and to give more control over the process.

Steps to implement:

  1. Import the requests module along with urllib3 or a similar library that can enforce a retry strategy.
  2. Define a custom retry strategy with backoff parameters.
  3. Mount this strategy onto your HTTP session.
  4. Make the HTTP request using this customized session.

Code example:


import requests
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter

retry_strategy = Retry(
    total=3,
    status_forcelist=[413, 429, 500, 502, 503, 504],
    method_whitelist=['HEAD', 'GET', 'OPTIONS']
)
adapter = HTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount('http://', adapter)
http.mount('https://', adapter)

try:
    response = http.get('http://example.com')
    print(response.content)
except requests.exceptions.RequestException as e:
    print(e)

Advantages: Provides a sophisticated method of handling redirects with increased control over the request process.
Limitations: More complex to set up and may introduce additional dependencies into your project.

Solution 4: Verify the URL

A common reason for the TooManyRedirects exception is an incorrect URL structure or server configuration leading to infinite redirection loops. Always verify that the URLs you’re using are correct and fix any errors at the source if possible.

Next Article: Resolving ConnectionError Exception in Python’s Requests Module

Previous Article: Resolving Timeout Exceptions in Python Requests Module

Series: Python: Network & JSON tutorials

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types