Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Requests · Class

Session

A Requests Session groups HTTP calls while persisting selected settings and cookies. It also provides connection pooling and can be used as a context manager for automatic cleanup.

How it works

Session groups repeated HTTP requests while retaining configuration and cookies between them. It also uses connection pooling, and its context-manager form closes the session when the block ends.

Syntax

requests.Session()

Key points

  • A Session carries selected request parameters from one request to later requests.
  • Cookies set through the session are retained for subsequent requests made by that session.
  • Connection pooling can reuse a TCP connection for repeated requests to the same host.
  • Session exposes the standard Requests HTTP methods, including methods for common verbs.
  • Authentication can be configured as a session-level default for requests issued through that session.

Common mistakes

  • Use a context manager when the session's lifetime should end after a bounded block, so its resources are closed automatically.
  • Manipulate Session.cookies with the cookie utility functions rather than assuming arbitrary cookie assignment handles all cookie behavior.
  • A session starts with adapters for HTTP and HTTPS; custom transport behavior should be mounted deliberately rather than assumed to apply automatically.

Example: Persist session configuration across requests

import requests

with requests.Session() as session:
    session.headers.update({"Accept": "application/json"})
    response = session.get("https://httpbin.org/get", timeout=10)
    response.raise_for_status()
    print(response.json()["url"])

Creates a session, applies a default header, performs a request, and closes the session automatically.

When to use it

Connection reuse

Several calls target the same host and should benefit from reusable connections.

Shared request configuration

Multiple requests share authentication, headers, or other defaults.

Cookie-based workflows

A workflow needs cookies from one response to be sent on later requests.

Scoped resource management

A short-lived request workflow should release resources automatically.

Interview questions

QWhat does a Requests Session provide beyond calling requests.get directly?

A Session persists selected request settings and cookies across requests. It also uses urllib3 connection pooling, allowing connections to the same host to be reused.

Follow-up: How should session-wide and request-specific headers interact? Session headers provide defaults, while headers supplied to an individual request can add or override them.

QCan a Session perform the usual Requests HTTP methods?

Yes. A Session has the methods of the main Requests API, so it can issue GET, POST, and other HTTP requests through the session object. This lets shared state and configuration apply consistently.

Follow-up: How can a session be safely bounded? Use with requests.Session() as session: so the session is closed when the block exits.

QHow should Session lifetime be managed?

Use with requests.Session() as session: when the session should be scoped to a block. This context-manager pattern ensures the session is closed after the block completes.

Follow-up: What state remains useful during that block? Cookies and configured defaults are available to subsequent requests made through the same session.

  • requests.Request
  • requests.Response
  • requests.Session.cookies
  • requests.adapters.HTTPAdapter
  • requests.sessions.session

Source: Session Objects — Requests: HTTP for Humans. Checked 2026-10-15.