[Python] TypeError: handle_connection() missing 1 required positional argument: 'path'

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

  1. Stack

    Stack Membro Participativo

    I have the following code.

    server.py:

    import asyncio
    import websockets
    from websockets.exceptions import ConnectionClosedError

    async def handle_connection(websocket, path):
    print("[+] Client connected.")
    try:
    async for message in websocket:
    print(f"[*] Message received: {message}")
    response = f"Server Echo: {message}"
    await websocket.send(response) # Send response to client
    except ConnectionClosedError as e:
    print(f"[-] Connection closed by client: {e}")
    except Exception as e:
    print(f"[!] Unexpected error: {e}")

    async def main():
    server = await websockets.serve(handle_connection, "0.0.0.0", 9000)
    print("[+] WebSocket server started on port 9000...")
    await server.wait_closed()

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


    client.py:

    import asyncio
    import websockets
    from websockets.exceptions import ConnectionClosedError, InvalidURI, InvalidHandshake

    async def test_websocket():
    uri = "ws://localhost:9000" # Make sure the port matches the server
    try:
    async with websockets.connect(uri) as websocket:
    print("[+] Connected to WebSocket Server")

    try:
    message = "Hello Server!"
    print(f"[*] Sending: {message}")
    await websocket.send(message)

    response = await websocket.recv()
    print(f"[+] Server Response: {response}")
    except ConnectionClosedError as e:
    print(f"[!] Connection Error: {e}")
    except (InvalidURI, InvalidHandshake) as e:
    print(f"[!] Error connecting to server: {e}")
    except Exception as e:
    print(f"[!] Unexpected error: {e}")

    asyncio.run(test_websocket())


    This is the error when I'm trying to run client.py:

    PS C:\Users\ruben\OneDrive\Ambiente de Trabalho\Honeypot> python client.py
    [+] Connected to WebSocket Server
    [*] Sending: Hello Server!
    [!] Connection Error: received 1011 (internal error); then sent 1011 (internal error)
    PS C:\Users\ruben\OneDrive\Ambiente de Trabalho\Honeypot>


    PS C:\Users\ruben\OneDrive\Ambiente de Trabalho\Honeypot> python server.py
    [+] WebSocket server started on port 9000...
    connection handler failed
    Traceback (most recent call last):
    File "C:\Users\ruben\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\websockets\asyncio\server.py", line 376, in conn_handler
    await self.handler(connection)
    ^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: handle_connection() missing 1 required positional argument: 'path'


    I already killed the process in the port 9000 and changed the port, assuming that is the problem, but I'm wrong. How can I fix this?

    Continue reading...

Compartilhe esta Página