Sep-23-2021, 09:26 AM
Hello,
using web socket server to send data to user
now , when I shut down the client usinc CTRL+C I get this error on the server:
1. why am I still seeing this line:
2. how do I handle the error ? and tell him to print it only one time?
this is the full code:
using web socket server to send data to user
now , when I shut down the client usinc CTRL+C I get this error on the server:
code = 1006 (connection closed abnormally [internal]), no reason Got request ('10.0.0.111', 53739) code = 1006 (connection closed abnormally [internal]), no reason Got request ('10.0.0.111', 53739) code = 1006 (connection closed abnormally [internal]), no reason Got request ('10.0.0.111', 53739) code = 1006 (connection closed abnormally [internal]), no reason Got request ('10.0.0.111', 53739) code = 1006 (connection closed abnormally [internal]), no reason Got request ('10.0.0.111', 53739) code = 1006 (connection closed abnormally [internal]), no reason Got request ('10.0.0.111', 53739)when I turn off the client with
ws.close()I get this:
code = 1000 (OK), no reason Got request ('10.0.0.111', 59060) code = 1000 (OK), no reason Got request ('10.0.0.111', 59060) code = 1000 (OK), no reason Got request ('10.0.0.111', 59060) code = 1000 (OK), no reason Got request ('10.0.0.111', 59060) code = 1000 (OK), no reason Got request ('10.0.0.111', 59060)2 questions:
1. why am I still seeing this line:
Got request ('10.0.0.111', 53739)nothing is connected to the server on both cases?
2. how do I handle the error ? and tell him to print it only one time?
this is the full code:
import asyncio import time import websockets from datetime import datetime import random TS = "TS" RN = "RN" async def David(websocket, path): while True: try: remote_ip = websocket.remote_address print('Got request ' + str(remote_ip)) await websocket.send(TS + "!" + str(RN)) await asyncio.sleep(1) except asyncio.exceptions.IncompleteReadError as e2: print(e2) except websockets.exceptions.ConnectionClosedError as E1: print(E1) except Exception as e: print(e) async def time_update(): while True: global TS TS = datetime.now().strftime('%d-%m-%y %H-%M-%S-%f') print(TS) await asyncio.sleep(0.742) async def randomNumber(): while True: global RN RN = random.randrange(0, 10000, 1) print(RN) await asyncio.sleep(0.217) async def main(): asyncio.get_event_loop().create_task(time_update()) asyncio.get_event_loop().create_task(randomNumber()) async with websockets.serve(David, "10.0.0.111", 8765): print("server started") await asyncio.Future() asyncio.run(main())thanks,