sseclient
sseclient copied to clipboard
Handling redirects, add origin to Event
Code comment:
python
# TODO: Ensure we're handling redirects. Might also stick the 'origin'
# attribute on Events like the Javascript spec requires.
We should ensure that's implemented.
Redirects are already implicitly handled, but we can improve upon that to make it explicitly clear. Test case demonstrating this below.
server.py:
from flask import Flask, redirect, Response
import time
app = Flask(__name__)
@app.route('/old-stream')
def old():
return redirect('/new-stream', code=301)
@app.route('/new-stream')
def new_stream():
def gen():
yield "data: hello\n\n"
while True:
time.sleep(1)
yield "data: ping\n\n"
return Response(gen(), mimetype='text/event-stream')
if __name__ == "__main__":
app.run(port=5050)
client.py:
from sseclient import SSEClient
messages = SSEClient('http://localhost:5050/old-stream')
for msg in messages:
print(msg)
output:
python switch-client.py
hello
ping
ping