[Python] Hang on websocket.recv() after... something?

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    Apologies for the ambiguous question - I actually have no idea what I changed that stopped this from working.

    I'm working on a text adventure engine (open source project) in Python using Websockets. This was working fine with the asyncio and websockets modules. I moved things around a bit and now it hangs upon receiving input.

    I lied - it never worked. This code however, does work:

    #!/usr/bin/python3

    import asyncio
    import websockets
    import time

    async def hello(websocket, path):
    while True:
    name = await websocket.recv()
    print("< {}".format(name))

    greeting = ''.join(name + "... " for x in range(5))
    await websocket.send(greeting)
    print("> {}".format(greeting))

    time.sleep(0.1);

    start_server = websockets.serve(hello, '', 26231)

    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()


    My code (the code that doesn't work) is available on the respective GitHub repo:

    https://github.com/KernelDeimos/mmota-server/issues/1

    Later when I have the time I'll add a repository for my client and link that here as well, though it's very basic (no changes were made to the client between the working commit and the broken one).

    The client connects to localhost:26231 and sends a message that I type (ex: "echo")

    The server sends back the message - or at least, it's supposed to.

    Unfortunately, the server hangs on this line of server.py:

    data = await self.websocket.recv()


    and the print on the next line is never executed.

    Continue reading...

Compartilhe esta Página