Answer the question
In order to leave comments, you need to log in
How to send request to websockets server? And what is better to use to handle long running requests on the server?
1. I wrote a client, but I don't understand how to send a message to the server for a test? ...
client.
import asyncio
import logging
import websockets
from websockets import WebSocketServerProtocol
import os
def log_message(message: str) -> None:
logging.info(f"Message: {message}")
async def consumer_handler(ws: WebSocketServerProtocol) -> None:
async for message in ws:
log_message(message)
async def consume(host: str, port: int) -> None:
ws_url = f"ws://{host}:{port}"
async with websockets.connect(ws_url) as ws:
await consumer_handler(ws)
ws.send("test test")
print(await ws.recv())
if __name__ == '__main__':
HOST = "server"
PORT = 8001
loop = asyncio.get_event_loop()
loop.run_until_complete(consume(host=HOST, port=PORT))
loop.run_forever()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question