Skip to content

An error occurs when using Jsonrpcserver over WebSocket with Python3.12 #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
8188 opened this issue Nov 14, 2024 · 5 comments
Closed

Comments

@8188
Copy link

8188 commented Nov 14, 2024

When using jsonrpcserver over WebSocket with Python 3.10, there is no problem, but with Python 3.12, an error occurs:

...\Lib\site-packages\websockets\asyncio\server.py", line 373, in conn_handler
await self.handler(connection)
^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: main() missing 1 required positional argument: 'path'

Below is my test codes:
server.py

import asyncio
from jsonrpcserver import method, Success, Result, async_dispatch, serve
import websockets

@method
async def ping() -> Result:
    return Success("pong")

async def main(websocket, path):
    if response := await async_dispatch(await websocket.recv()):
        await websocket.send(response)

async def start_server():
    server = await websockets.serve(main, "localhost", 8765)
    await server.wait_closed()

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

client.py

import asyncio
import websockets

async def send_ping():
    uri = "ws://localhost:8765"
    async with websockets.connect(uri) as websocket:
        request = '{"jsonrpc": "2.0", "method": "ping", "id": 1}'
        await websocket.send(request)
        response = await websocket.recv()
        print(f"Received: {response}")

if __name__ == "__main__":
    asyncio.run(send_ping())
@bcb
Copy link
Member

bcb commented Nov 14, 2024

Try the example here:
https://github.com/explodinglabs/jsonrpcserver/blob/main/examples/websockets_server.py

I just tried it with Python 3.12.6 and it worked fine.

@8188
Copy link
Author

8188 commented Nov 15, 2024

Thanks a lot, @bcb. Using websockets.server.serve instead of websockets.serve resolved the bug, but still getting a DeprecationWarning. I use warnings.filterwarnings("ignore", category=DeprecationWarning) to suppress it, not elegant solution, though.

@bcb
Copy link
Member

bcb commented Nov 15, 2024

Is it a warning from jsonrpcserver or websockets?

@8188
Copy link
Author

8188 commented Nov 15, 2024

From websockets @bcb

DeprecationWarning: websockets.server.WebSocketServerProtocol is deprecated
from websockets.server import WebSocketServerProtocol, serve
DeprecationWarning: websockets.server.serve is deprecated
from websockets.server import WebSocketServerProtocol, serve

@bcb
Copy link
Member

bcb commented Nov 15, 2024

It looks like the usage of the websockets library has changed in recent versions. I should update the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants