How to use Session object with Python Requests

Python’s Requests library is a popular tool for handling HTTP requests, and its Session object offers a powerful way to persist parameters across multiple requests to the same website. By reusing open TCP connections, the Session object significantly boosts performance and simplifies your code. Let’s explore how to work with the Session object and why it’s an essential part of Python web development.

What is the Requests Library in Python?

The Requests Library is a widely-used Python library that simplifies sending HTTP requests. It provides a clean, readable API for:

  • Sending GET, POST, PUT, and DELETE requests.
  • Posting JSON and XML data.
  • Submitting HTML forms.
  • Uploading files.

While Python has the built-in urllib3 module for HTTP requests, most developers prefer Requests for its simplicity and ease of use.

How to Install the Requests Library?

To start using the Requests library, install it with the following command:

pip install requests  

Once installed, import it into your Python code:

import requests  

What is a Python Session Object?

The Session object in the Requests library helps persist parameters, such as headers and cookies, across multiple HTTP requests. It allows you to:

  • Reuse TCP connections for improved performance.
  • Store custom headers and authentication data.
  • Maintain cookies throughout a session.
  • Send multiple requests without repeatedly defining the same parameters.

Creating a Session Object:

session = requests.Session()  

The Session object retains all methods available in the main Requests API.

Why Use a Session Object?

  1. Performance: Reduces overhead by reusing open TCP connections.
  2. Convenience: Automatically persists headers, cookies, and other parameters across requests.
  3. Consistency: Maintains a uniform configuration for all requests in a session.

How to Set Headers in a Session Object?

Custom headers are often required when interacting with APIs or websites. Here’s how to set them in a session:

session.headers.update({  
    'User-Agent': 'my-app',  
    'Authorization': 'Bearer YOUR_TOKEN'  
})  
response = session.get('https://api.example.com/data')  

Adding Authentication to a Session Object

You can include authentication credentials for all requests in a session:

session.auth = ('username', 'password')  
response = session.get('https://api.example.com/secure-data')  

Setting Proxies in a Session Object

If you need to route your requests through a proxy, configure it like this:

session.proxies = {  
    'http': 'http://proxy.example.com:8080',  
    'https': 'https://proxy.example.com:8443'  
}  
response = session.get('https://api.example.com')  

How to Add Cookies to a Session Object?

The Session object can store cookies, which are useful for maintaining user sessions or tracking activity:

session.cookies.set('session_id', '123456')  
response = session.get('https://example.com/dashboard')  

Passing an Authorization Header

You can include an authorization header in your session for APIs that require token-based authentication:

session.headers.update({  
    'Authorization': 'Bearer YOUR_ACCESS_TOKEN'  
})  
response = session.get('https://api.example.com/user')  

Session vs. Cookies: Key Differences

FeatureSessionCookies
Storage LocationStored on both client and serverStored on the client’s browser
PurposeTracks user activity during a sessionTracks usage and stores small amounts of data
ExpirationEnds when the user logs out or session times outPersists based on expiry time set by server
SecurityServer-side storage provides better securityClient-side storage is less secure

Example:

  • A session stores login state server-side, while cookies store user preferences like language or theme client-side.

How to Use Session Object for Multiple Requests

A practical use case for the Session object is maintaining a login session across multiple requests.

session = requests.Session()  

# Login  
login_url = 'https://example.com/login'  
credentials = {'username': 'user', 'password': 'pass'}  
session.post(login_url, data=credentials)  

# Access protected page  
dashboard_url = 'https://example.com/dashboard'  
response = session.get(dashboard_url)  
print(response.text)  

Advantages of Using the Session Object

  1. Code Efficiency: No need to pass headers or cookies repeatedly.
  2. Performance Boost: Reuses connections for faster requests.
  3. Streamlined Workflow: Simplifies interaction with APIs requiring consistent configurations.

Python’s Requests Session object is a robust feature for handling multiple HTTP requests efficiently and conveniently. By persisting parameters like headers, cookies, and authentication data, the Session object improves performance and reduces redundant code. Whether you’re building web scrapers or interacting with APIs, mastering the Session object is a must for Python developers.

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *