python requests follow all redirects

Python Requests Follow All Redirects

Python Requests is a powerful library that allows developers to send HTTP/1.1 requests extremely easily. However, when working with web applications, it's often necessary to follow redirects. In this article, we will explore how to use Python Requests to follow all redirects.

Following Redirects with Requests

By default, Requests will automatically follow redirects for 30 times. This can be customized by setting the `max_redirects` parameter when sending a request. To follow all redirects, we simply need to set `allow_redirects` to `True`.

    import requests

    response = requests.get('', allow_redirects=True)


The code above sends a GET request to `` and allows Requests to follow all redirects. The response of the request is then printed to the console.

Handling Redirect Loops

Sometimes, web applications may return a redirect that points to the same URL. This can cause an infinite loop if we're not careful. To handle this situation, we can set a maximum number of redirects using the `max_redirects` parameter.

    import requests

    response = requests.get('', allow_redirects=True, max_redirects=10)


In the code above, we set a maximum of 10 redirects before giving up. If the web application returns more than 10 redirects, Requests will raise a `TooManyRedirects` exception.