python network_error ai_generated true

requests.exceptions.TooManyRedirects: Exceeded 30 redirects while fetching 'https://redirect.example.com'

ID: python/requests-connectionerror-too-many-redirects

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-09-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The server is redirecting the client in a loop or too many times.

generic

中文

服务器在循环中重定向客户端或重定向次数过多。

Workarounds

  1. 85% success Manually handle redirects by inspecting the Location header
    response = requests.get('https://redirect.example.com', allow_redirects=False)
    while response.is_redirect:
        next_url = response.headers['Location']
        response = requests.get(next_url, allow_redirects=False)
        if response.status_code == 200:
            break
  2. 80% success Use a session with a custom redirect handler to detect loops
    from requests import Session
    s = Session()
    s.max_redirects = 10
    response = s.get('https://redirect.example.com')

Dead Ends

Common approaches that don't work:

  1. Disabling redirects entirely 70% fail

    May miss the final target if redirects are legitimate.

  2. Increasing the maximum redirect limit arbitrarily 80% fail

    If it's a loop, increasing limit does not help; it will still exceed.