reconnect-fail.py (750B)
1 def main(request, response): 2 name = b"recon_fail_" + request.GET.first(b"id") 3 4 headers = [(b"Content-Type", b"text/event-stream")] 5 cookie = request.cookies.first(name, None) 6 state = cookie.value if cookie is not None else None 7 8 if state == b'opened': 9 status = (200, b"RECONNECT") 10 response.set_cookie(name, b"reconnected"); 11 body = b"data: reconnected\n\n"; 12 13 elif state == b'reconnected': 14 status = (204, b"NO CONTENT (CLOSE)") 15 response.delete_cookie(name); 16 body = b"data: closed\n\n" # Will never get through 17 18 else: 19 status = (200, b"OPEN"); 20 response.set_cookie(name, b"opened"); 21 body = b"retry: 2\ndata: opened\n\n"; 22 23 return status, headers, body