websocket api python on close

Solutions on MaxInterview for websocket api python on close by the best coders in the world

showing results for - "websocket api python on close"
Solène
04 Apr 2020
1import asyncio
2import datetime
3import random
4import websockets
5
6async def time(websocket, path):
7    while True:
8        now = datetime.datetime.utcnow().isoformat() + "Z"
9        try:
10            await websocket.send(now)
11        except websockets.exceptions.ConnectionClosed:
12            print("Client disconnected.  Do cleanup")
13            break             
14        await asyncio.sleep(random.random() * 3)
15
16start_server = websockets.serve(time, "127.0.0.1", 5678)
17
18asyncio.get_event_loop().run_until_complete(start_server)
19asyncio.get_event_loop().run_forever()
20