[Python] Python requests POST request hangs, when used with websockets library [duplicate]

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I'm using Python requests to send a POST request, while concurrently maintaining a WebSocket connection using the websockets library:

    import asyncio
    import requests
    import websockets

    async def websocket_handler(uri):
    async with websockets.connect(uri) as websocket:
    while True:
    message = await websocket.recv()
    print(f"Received message: {message}")

    async def make_request():
    s = requests.Session()
    print(1)
    try:
    response = s.post('http://192.168.1.4/create', timeout=10)
    print(3)
    print(response.text)
    except requests.exceptions.Timeout:
    print("The request timed out") # this happens
    except requests.exceptions.RequestException as e:
    print(f"An error occurred: {e}")

    async def main():
    websocket_task = asyncio.create_task(websocket_handler("ws://example.com/websocket"))
    request_task = asyncio.create_task(make_request())
    await asyncio.gather(websocket_task, request_task)

    if __name__ == "__main__":
    asyncio.run(main())


    The server logs show no request. The server is reachable and correctly configured. What could be causing this issue when using requests with websockets?

    Continue reading...

Compartilhe esta Página