Skip to content

Session Management ## Business Purpose

Allow multiple requests to share configuration, cookies, and connection pools, improving performance and statefulness across interactions. ## Current Behaviors - Session class inherits from SessionRedirectMixin src/requests/sessions.py:127. - __init__ sets default headers, auth, proxies, hooks, params, stream, verify, cert, max_redirects, trust_env, cookies, and mounts default HTTP and HTTPS adapters src/requests/sessions.py:442. - prepare_request merges session cookies with request cookies and prepares a PreparedRequest src/requests/sessions.py:511. - send method sets default values for stream, verify, cert, and proxies from session attributes, handles redirects and cookie extraction src/requests/sessions.py:752. - merge_environment_settings reads REQUESTS_CA_BUNDLE and CURL_CA_BUNDLE environment variables src/requests/sessions.py:831. - close method calls close() on all adapters src/requests/sessions.py:883. - mount registers an adapter for a prefix and reorders adapters by descending prefix length src/requests/sessions.py:888. ## Technical Implementation Session uses HTTPAdapter for HTTP/HTTPS. Hooks are merged via merge_hooks and merge_setting src/requests/sessions.py:76. Redirect resolution follows the resolve_redirects generator, which respects max_redirects and strips auth on host changes. ## Definition of Done - Consecutive requests via a session share cookies set by the first response. - Session-level headers are present in all requests unless overridden. - A session maintains a connection pool and does not create a new TCP connection for each request to the same host. - Closing a session releases all connections back to the pool. - session.get_adapter('http://example.com') returns the most specific matching adapter.