Server-Sent Events (SSE) in Python

Server-Sent Events (SSE) in Python

In order to subscribe to the Server-Sent Events (SSE) in Python, we need sseclient. So, install it using the following command.

python3 -m pip install sseclient

In this example, we are listening to the Wikipedia’s Server-Sent Events.

Sample Code:

import sseclient

url = 'https://stream.wikimedia.org/v2/stream/recentchange'

messages = sseclient.SSEClient( url )

for msg in messages:
    print( msg )
    strRequest = input(
        'Do you want to continue? (y/n): '
    )
    if strRequest == 'n':
        messages.resp.close()
        break

Output:

Leave a Reply