Sling Academy
Home/Python/Python Requests module: Tracking redirection and history

Python Requests module: Tracking redirection and history

Last updated: January 02, 2024

Introduction

Understanding HTTP redirection is key when making web requests. The Python Requests module simplifies these tasks, allowing you to track redirections and the request history with ease.

Getting Started with the Requests Module

To begin, you need the Requests module installed in your Python environment:

pip install requests

Once installed, you can start making HTTP requests. Here’s a basic example:

import requests

response = requests.get('http://example.com')
print(response.status_code)
print(response.url)

Understanding Redirection

Some HTTP requests lead to redirections. The server responds with a header telling the client to look somewhere else.

Here’s how to check if a response was redirected:

response = requests.get('http://example.com/some-redirect-url')
if response.history:
    print("Request was redirected")
for resp in response.history:
    print(resp.status_code, resp.url)

And to get the final URL:

print("Final destination:", response.url)

Handling Redirection Manually

If you require control over redirection, disable it as follows:

response = requests.get('http://example.com/some-redirect', allow_redirects=False)
print(response.status_code)

To follow the redirect manually:

if response.status_code == 302:
    new_url = response.headers['Location']
    follow_up_resp = requests.get(new_url)
    print(follow_up_resp.url)

Advanced Handling of Redirection Chains

For more complex redirection scenarios, such as capturing cookies or detailed information throughout the redirection chain, use session objects:

with requests.Session() as s:
    response = s.get('http://example.com/some-redirect')
    for resp in response.history:
        print(resp.status_code, resp.url, resp.cookies)

Special types of redirection

HTTP status codes like 301, 302, and 307 represent different types of redirection. You can adapt to each type:

if response.status_code == 301:
    # Permanent redirection logic
elif response.status_code == 302:
    # Temporary redirection logic
elif response.status_code == 307:
    # Redirect but keep the method as POST

Error Handling

When tracking redirection, errors might happen. Ensure robust code by handling them:

try:
    response = requests.get('http://example.com/bad-redirect')
    response.raise_for_status()
except requests.exceptions.HTTPError as errh:
    print ("An Http Error occurred:\n", errh)
except requests.exceptions.ConnectionError as errc:
    print ("An Error Connecting to the API occurred:\n", errc)
except requests.exceptions.Timeout as errt:
    print ("A Timeout Error occurred:\n", errt)
except requests.exceptions.RequestException as err:
    print ("An Unknown Error occurred:\n", err)

Advanced Redirections with Parameters

By using session objects, you can pass along custom headers, authentication, and more across redirects:

payload = {'key1': 'value1', 'key2': 'value2'}
headers = {'user-agent': 'my-app/0.0.1'}

with requests.Session() as s:
    s.headers.update(headers)
    response = s.get('http://httpbin.org/redirect/1', params=payload)
    for resp in response.history:
        print(resp.request.headers, resp.url)

Summary

Mastering the Requests module for tracking HTTP redirections enhances the robustness and efficiency of your web interaction scripts. This tutorial provided foundational knowledge and techniques to adeptly navigate and utilize HTTP redirections in Python.

Next Article: Python: Using aiohttp to crawl webpages asynchronously

Previous Article: Python Requests module: Print response status code and headers

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