When making requests with the popular Httpx HTTP client library in Python, you may want to persist cookies and session data across multiple requests. Here's a practical guide on utilizing Httpx's session support to maintain state.
Creating a Session
To create a persistent session in Httpx, instantiate the
from httpx import Client, Session
session = Session()
client = Client(session=session)
This binds the client to the session. Now any requests made with
Session Usage
With a session-enabled client, you can make multiple requests that share the same state:
resp = client.get("https://example.com/login")
# server sets login session cookie
resp = client.get("https://example.com/user-page")
# session cookie automatically sent, allows access
Sessions persist cookies, headers, connections, and other metadata across requests. This is very useful for:
Session Expiration
By default, sessions have no expiration and persist indefinitely. However, servers can set explicit cookie expiration times.
To clear a session manually, call the
session.close()
This resets the session to its initial blank state.
Configuring Session Behavior
There are a few options to control session persistence behavior:
session = Session(
http2=True, # enable HTTP/2 support
trust_env=False # disable proxy support
)
The key behaviors to understand are:
Use Cases
Some examples of using Httpx sessions:
Sessions allow mimicking real-world usage patterns.
Key Takeaways
By leveraging sessions, you can write simpler stateful HTTP code in Python.