Skip to main content

Python JSON-RPC Library Client Server - Simple To Use Python JSONRPC-Library

Project description

Install:

pip install bunch
pip install python-jsonrpc

Library Usage Example:

#!/usr/bin/env python
# coding: utf-8

import pyjsonrpc


def add(a, b):
    """Test function"""
    return a + b


# 1. Initialize JSON-RPC class with JSON-RPC method(s)
rpc = pyjsonrpc.JsonRpc(methods = {"add": add})

# 2. Create JSON-RPC string with parameters (= request string)
request_json = pyjsonrpc.create_request_json("add", 1, 2)
# request_json = '{"method": "add", "params": [1, 2], "id": "...", "jsonrpc": "2.0"}'

# 3. Call the JSON-RPC function and get back the JSON-RPC result (= response string)
response_json = rpc.call(request_json)
# response_json = '{"result": 3, "id": "...", "jsonrpc": "2.0"}'

# 4. Convert JSON-RPC string to Python objects
response = pyjsonrpc.parse_response_json(response_json)

# 5. Print result or error
if response.error:
    print "Error:", response.error.code, response.error.message
else:
    print "Result:", response.result

HTTP Client Example:

#!/usr/bin/env python
# coding: utf-8

import pyjsonrpc

http_client = pyjsonrpc.HttpClient(
    url = "http://example.com/jsonrpc",
    username = "Username",
    password = "Password"
)
print http_client.call("add", 1, 2)
# Result: 3

# It is also possible to use the *method* name as *attribute* name.
print http_client.add(1, 2)
# Result: 3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-jsonrpc-0.3.1.tar.gz (7.5 kB view details)

Uploaded Source

File details

Details for the file python-jsonrpc-0.3.1.tar.gz.

File metadata

File hashes

Hashes for python-jsonrpc-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b4be5a14503fff8879d5d33b12e8879ffa5e59f48b9de13dc5ff9099dda9afa2
MD5 49b3262f6a8fde2323c40bce1613938d
BLAKE2b-256 91a01f3ec80c1717d6a13dd11dc20093cfcf4a74498fb290045285ffab4e76e6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page