Master Python Requests: Auth, Cookies & Sessions

Python's Requests module is a powerful library that makes HTTP requests simple and efficient. This article will guide you through handling authentication, cookies, and sessions like a pro, ensuring your web scraping and API interactions are effective and secure.

Table of Contents


Requests support several types of authentication methods, including Basic, Digest, and OAuth. We'll focus on Basic and Digest authentication.

Basic Authentication

Basic authentication is the simplest form of authentication. It transmits the username and password as a Base64-encoded string in the Authorization header. Here's an example:

import requests

response = requests.get('', auth=('username', 'password'))


Digest Authentication

Digest authentication is similar to Basic authentication but adds a layer of security by hashing the password. Requests provide a built-in HTTPDigestAuth class to handle this:

from requests.auth import HTTPDigestAuth
import requests

response = requests.get('', auth=HTTPDigestAuth('username', 'password'))



Cookies are small pieces of data stored on the user's computer by the web browser. They are used to maintain state and track user activities. Requests can send and receive cookies using the cookies attribute of a response object.

Sending Cookies

To send cookies, you can use the cookies parameter in a request:

cookies = {'session_id': '123456'}

response = requests.get('', cookies=cookies)


Receiving Cookies

To access the cookies returned by a server, you can use the cookies attribute of a response object:

response = requests.get('')



A session is a way to persist certain parameters across multiple requests. It can be useful for maintaining authentication and cookies throughout your interactions with a website. Requests provide a Session class to manage sessions.

Creating a Session

To create a session, you can use the Session class:

import requests

session = requests.Session()

Using a Session

Once you have created a session, you can use it to make requests. The session will persist authentication and cookies across the requests:

session.auth = ('username', 'password')

response = session.get('')


Closing a Session

It's a good practice to close a session when you're done using it. You can do this with the close method:


Alternatively, you can use the with statement to automatically close the session:

with requests.Session() as session:
    session.auth = ('username', 'password')
    response = session.get('')



In this article, you learned how to handle authentication, cookies, and sessions using Python's Requests module. With these techniques in your arsenal, you'll be well-equipped to create efficient and secure web scraping and API interactions. Happy coding!

An AI coworker, not just a copilot

View VelocityAI