Package: jsonrpc-websocket
This is a compact and simple JSON-RPC websocket client implementation for asyncio python code. This code is forked fromhttps://github.com/gciotta/jsonrpc-requestsMain FeaturesSupports nested namespaces (eg.app.users.getUsers())100% test coverageUsageIt is recommended to manage the aiohttp ClientSession object externally and pass it to the Server constructor.(See the aiohttp documentation.)If not passed to Server, a ClientSession object will be created automatically, and will be closed when the websocket connection is closed. If you pass in an external ClientSession, it is your responsibility to close it when you are finished.Execute remote JSON-RPC functionsimportasynciofromjsonrpc_websocketimportServerasyncdefroutine():server=Server('ws://localhost:9090')try:awaitserver.ws_connect()awaitserver.foo(1,2)awaitserver.foo(bar=1,baz=2)awaitserver.foo({'foo':'bar'})awaitserver.foo.bar(baz=1,qux=2)finally:awaitserver.close()asyncio.get_event_loop().run_until_complete(routine())A notificationimportasynciofromjsonrpc_websocketimportServerasyncdefroutine():server=Server('ws://localhost:9090')try:awaitserver.ws_connect()awaitserver.foo(bar=1,_notification=True)finally:awaitserver.close()asyncio.get_event_loop().run_until_complete(routine())Handle requests from server to clientimportasynciofromjsonrpc_websocketimportServerdefclient_method(arg1,arg2):returnarg1+arg2asyncdefroutine():server=Server('ws://localhost:9090')# client_method is called when server requests method 'namespace.client_method'server.namespace.client_method=client_methodtry:awaitserver.ws_connect()finally:awaitserver.close()asyncio.get_event_loop().run_until_complete(routine())Pass through arguments to aiohttp (see alsoaiohttp  documentation)importasyncioimportaiohttpfromjsonrpc_websocketimportServerasyncdefroutine():server=Server('ws://localhost:9090',auth=aiohttp.BasicAuth('user','pass'),headers={'x-test2':'true'})try:awaitserver.ws_connect()awaitserver.foo()finally:awaitserver.close()asyncio.get_event_loop().run_until_complete(routine())Pass through aiohttp exceptionsimportasyncioimportaiohttpfromjsonrpc_websocketimportServerasyncdefroutine():server=Server('ws://unknown-host')try:awaitserver.ws_connect()awaitserver.foo()exceptTransportErrorastransport_error:print(transport_error.args[1])# this will hold a aiohttp exception instancefinally:awaitserver.close()asyncio.get_event_loop().run_until_complete(routine())TestsInstall the Python tox package and runtox, it’ll test this package with various versions of Python.Changelog3.1.5 (2024-01-16)Add explicit dependency to async-timeout(#13)@miettal3.1.4 (2022-05-23)Only reconnect session when the session is managed internallyRemove deprecated with timeout syntax3.1.3 (2022-05-23)Fix unclosed client session bug(#12)@Arjentix3.1.2 (2022-05-03)Unpin test dependencies3.1.1 (2021-11-21)Fixed compatibility with async_timeout 4.03.1.0 (2021-05-03)Bumped jsonrpc-base to version 2.1.03.0.0 (2021-03-17)Bumped jsonrpc-base to version 2.0.0BREAKING CHANGE:Allow single mapping as a positional parameter.Previously, when calling with a single dict as a parameter (example:server.foo({'bar':0})), the mapping was used as the JSON-RPC keyword parameters. This made it impossible to send a mapping as the first and only positional parameter. If you depended on the old behavior, you can recreate it by spreading the mapping as your method’s kwargs. (example:server.foo(**{'bar':0}))2.0.0 (2020-12-22)Remove session as a reserved attribute on Server1.2.1 (2020-09-11)Fix loop not closing after client closes1.2.0 (2020-08-24)Support for async server request handlers1.1.0 (2020-02-17)Support servers that send JSON-RPC requests as binary messages encoded with UTF-8(#5)@shiaky1.0.2 (2019-11-12)Bumped jsonrpc-base to version 1.0.31.0.1 (2018-08-23)Bumped jsonrpc-base to version 1.0.21.0.0 (2018-07-06)Bumped jsonrpc-base to version 1.0.10.6 (2018-03-11)Minimum required version of aiohttp is now 3.0.Support for Python 3.4 is now dropped.Credits@gciottafor creating the base projectjsonrpc-requests.@mbroadstfor providing full support for nested method calls, JSON-RPC RFC
compliance and other improvements.@vaabfor providing api and tests improvements, better RFC compliance.