Remove the while True loop from handler hello, because this function is called from
Server-code:
just assigned loop and using upper case letters for constants.
websockets.serve
for each new connection.Server-code:
import asyncio import websockets import time from datetime import datetime async def hello(websocket, path): print('Got request') task = await websocket.recv() remote_ip = websocket.remote_address print(task) if "29" in task: print('this is a good reading from ' + str(remote_ip)) await websocket.send('Good question!') await websocket.send('Wrong question!') start_server = websockets.serve(hello, "localhost", 6500) loop = asyncio.get_event_loop() loop.run_until_complete(start_server) # this for startup the websocket server print("server started") loop.run_forever()Client-code:
just assigned loop and using upper case letters for constants.
import asyncio import websockets import time MESSAGE = "test 29" URI1 = "ws://localhost:6500/" async def hello(): try: async with websockets.connect(URI1) as websocket: await websocket.send(MESSAGE) replay = await websocket.recv() print (replay) except Exception as e: print (e) print ('problem sending') loop = asyncio.get_event_loop() loop.run_until_complete(hello())
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!