Metadata-Version: 2.5
Name: sseclient-py
Version: 2.1.0
Summary: Server-Sent Events client for Python
Project-URL: Repository, https://github.com/mpetazzoni/sseclient
Project-URL: Issues, https://github.com/mpetazzoni/sseclient/issues
Project-URL: PyPI, https://pypi.org/project/sseclient-py/
Author-email: Maxime Petazzoni <maxime.petazzoni@bulix.org>
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: eventsource,server-sent-events,sse
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Internet :: WWW/HTTP
Requires-Python: >=3.11
Description-Content-Type: text/x-rst

Server-Sent Events client for Python
====================================

``sseclient-py`` turns a streaming HTTP response into Server-Sent Events (SSE).
It works with ``requests``, ``urllib3`` and ``httpx``, and has no dependencies.
Python 3.11 or newer is required.

Installation
------------

.. code:: console

    $ pip install sseclient-py

Usage
-----

.. code:: python

    import requests
    import sseclient

    url = "https://example.com/events"
    with requests.get(
        url,
        headers={"Accept": "text/event-stream"},
        stream=True,
        timeout=(3.05, 60),
    ) as response:
        response.raise_for_status()
        for event in sseclient.SSEClient(response).events():
            print(event.id, event.event, event.data)

The timeout is a connect/read timeout, not a limit on the whole stream. Adjust
the read timeout to allow for your server's keep-alive interval.

Other HTTP clients
------------------

Pass the client's byte stream to ``SSEClient``:

.. code:: python

    import httpx
    import urllib3

    # urllib3
    http = urllib3.PoolManager()
    response = http.request("GET", url, preload_content=False)
    client = sseclient.SSEClient(response.stream())

    # httpx
    with httpx.stream("GET", url) as response:
        client = sseclient.SSEClient(response.iter_bytes())
        for event in client.events():
            print(event.data)

Reconnecting
------------

Manage connections and retries in your application. Reconnect when ``events()``
ends or the HTTP client raises a connection or read-timeout error. Use a delay
or backoff between attempts.

Resuming depends on the server. When ``event.id is not None``, remember that ID,
including an empty string (which clears it). Send a non-empty remembered ID in
the next request's ``Last-Event-ID`` header. ID-only updates are not exposed by
this parser; duplicate-free delivery is not guaranteed.

The yielded events have ``id``, ``event``, ``data`` and ``retry`` attributes.
Comments and events without data are ignored.

Resources
---------

- `SSE specification <https://html.spec.whatwg.org/multipage/server-sent-events.html>`_
- `PyPI <https://pypi.org/project/sseclient-py/>`_
- `Source and issues <https://github.com/mpetazzoni/sseclient>`_
