日別アーカイブ: 2025年4月8日

pythonのWebsocketの引数は現在async def handler(websocket):

環境

websockets 15.0.1

(.venv) PS D:\develop\test> pip show websockets
Name: websockets
Version: 15.0.1
Summary: An implementation of the WebSocket Protocol (RFC 6455 & 7692)
Home-page: https://github.com/python-websockets/websockets
Author:
Author-email: Aymeric Augustin <aymeric.augustin@m4x.org>
License: BSD-3-Clause
Location: d:\develop\test\.venv\Lib\site-packages
Requires:
Required-by:

10.1でなくなって13から非推奨になっていた模様(こちら

正しい呼び方

async def handler(websocket): //pathがない
    print("Client connected!")
    connected_clients.add(websocket)

正しくない呼び方

async def handler(websocket, path): // pathがある
    print("Client connected!")
    connected_clients.add(websocket)

その時のエラー

connection handler failed
Traceback (most recent call last):
File "d:\develop\test\.venv\Lib\site-packages\websockets\asyncio\server.py", line 376, in conn_handler
await self.handler(connection)
~~~~~~~~~~~~^^^^^^^^^^^^
TypeError: handler() missing 1 required positional argument: 'path'

server.py”, line 376でインスタンス化できない。

chatgptに騙されたのでメモ。