Persisting Sessions with Httpx in Python

Feb 5, 2024 ยท 2 min read

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 Client with a Session object:

from httpx import Client, Session

session = Session()
client = Client(session=session)

This binds the client to the session. Now any requests made with client will automatically save cookies set by the server, and send those cookies in subsequent requests.

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:

  • Logging into websites and scraping data
  • Interacting with APIs that require authorization
  • Testing workflows that involve multiple requests
  • 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 .close() method:

    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:

  • Cookie Persistence - Sessions maintain cookies across requests automatically
  • Connection Re-use - Sessions re-use connection pools for performance
  • Timeout Handling - Idle sessions don't close connections until expired
  • Use Cases

    Some examples of using Httpx sessions:

  • Web Scraping - Log into a website, scrape user profile pages
  • API Testing - Get an OAuth token, make authenticated API requests
  • Browser Automation - Emulate browser cookies and workflows with automation
  • Sessions allow mimicking real-world usage patterns.

    Key Takeaways

  • Httpx Sessions provide client-side state persistence for cookies, headers, connections
  • Enable sessions via the Client(session=...) constructor
  • Sessions improve performance and can emulate browser state
  • Clear sessions with .close() or reset the session instance
  • By leveraging sessions, you can write simpler stateful HTTP code in Python.

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: