jsonrpclib
jsonrpclib is a Python client library implementing the JSON-RPC v2.0 specification, with backwards compatibility considerations. It focuses on providing client-side functionality for interacting with JSON-RPC servers. The current version is 0.2.1. The project has seen limited updates since 2021; for more active development or server-side functionality, consider `jsonrpclib-pelix`.
Warnings
- breaking Python 2 support was officially dropped in version 0.1.0. Older versions of `jsonrpclib` were Python 2 compatible, but the current maintainer has transitioned to Python 3 exclusively.
- gotcha jsonrpclib is a client-only library. It does not provide functionality for implementing a JSON-RPC server. Attempts to use it for server-side logic will fail.
- gotcha The `jsonrpclib` project has not seen significant updates or active maintenance since April 2021. For new projects or if active development, bug fixes, or server-side functionality are critical, consider using `jsonrpclib-pelix`.
Install
-
pip install jsonrpclib
Imports
- Server
from jsonrpclib import Server
Quickstart
import os
from jsonrpclib import Server
# This quickstart demonstrates client-side usage.
# A JSON-RPC server must be running at the specified URL for this code to connect and execute.
# For demonstration purposes, we use a placeholder URL.
# Replace with your actual server URL or set via environment variable.
SERVER_URL = os.environ.get('JSONRPCLIB_SERVER_URL', 'http://localhost:8080/jsonrpc')
try:
# Create a server proxy
server = Server(SERVER_URL)
# Example: Call a simple method (e.g., 'add' with two arguments)
# The actual methods available depend on the connected JSON-RPC server.
print(f"Attempting to call 'add(5, 3)' on {SERVER_URL}...")
# This call will fail if no server is running or if the method doesn't exist.
result_add = server.add(5, 3)
print(f"Result of server.add(5, 3): {result_add}")
# Example: Access a method within a namespace (if supported by the server)
# e.g., 'system.get_info()'
print(f"Attempting to call 'system.get_info()' on {SERVER_URL}...")
# This call will fail if no server is running or if the namespace/method doesn't exist.
system_info = server.system.get_info()
print(f"System info from server.system.get_info(): {system_info}")
except Exception as e:
print(f"Error connecting to or calling JSON-RPC server at {SERVER_URL}: {e}")
print("Please ensure a JSON-RPC server is running at the specified URL and provides the called methods.")