jsonrpclib

0.2.1 · maintenance · verified Mon Apr 13

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

Install

Imports

Quickstart

This example shows how to create a client proxy and call methods on a remote JSON-RPC server. It requires a server to be running at `SERVER_URL` for the calls to succeed. Error handling for connection issues is included.

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.")

view raw JSON →