[Python] Unable to catch asyncio.IncompleteReadError for websocket server

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am currently working on a websocket server to allow communication between 2 computers, and I'm coding the communication between PC and server. I made a command to stop the server remotely, but when the connection is lost the script doesn't stop, I want to fix that. The code:

    try:
    #Allow to receive mutliple messages from server
    while True:
    response = await asyncio.wait_for(websocket.recv(), timeout=1)
    print(color + response + colorama.Style.RESET_ALL)
    except asyncio.TimeoutError:
    print("No response from server")
    if must_restart == True:
    return 'restart'
    except asyncio.IncompleteReadError:
    print("Incomplete read, connection might be closed")
    return
    except websockets.exceptions.ConnectionClosedError:
    print("Connection closed")
    return


    When running in debug mode (otherwise I don't get it), I get this exception :

    asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 2 expected bytes

    The above exception was the direct cause of the following exception:

    File "C:\Users\stard\OneDrive\Documents\code\Coomunication\com.py", line 131, in send_message
    response = await asyncio.wait_for(websocket.recv(), timeout=1)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\stard\OneDrive\Documents\code\Coomunication\com.py", line 257, in Client
    result = await send_message(list(machines.keys())[sel],list(machines.items())[sel][1])
    File "C:\Users\stard\OneDrive\Documents\code\Coomunication\com.py", line 291, in <module>
    asyncio.run(Client())
    websockets.exceptions.ConnectionClosedError: no close frame received or sent


    I put an except.IncompleteReadError: so why isn't the exception caught? Is this beacuse of asyncio.wait_for? If it is, what is the solution?

    Continue reading...

Compartilhe esta Página