Skip to main content
https://travis-ci.org/gerold-penz/python-jsonrpc.svg?branch=master

Installation

pip install python-jsonrpc

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

# Notifications send messages to the server, without response.
http_client.notify("add", 3, 4)

HTTP Server Example

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

import pyjsonrpc


class RequestHandler(pyjsonrpc.HttpRequestHandler):

    @pyjsonrpc.rpcmethod
    def add(self, a, b):
        """Test method"""
        return a + b


# Threading HTTP-Server
http_server = pyjsonrpc.ThreadingHttpServer(
    server_address = ('localhost', 8080),
    RequestHandlerClass = RequestHandler
)
print "Starting HTTP server ..."
print "URL: http://localhost:8080"
http_server.serve_forever()

CGI Example

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

import pyjsonrpc

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

# Handles the JSON-RPC request and gets back the result to STDOUT
pyjsonrpc.handle_cgi_request(methods = dict(add = add))

Library Usage Example

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

import pyjsonrpc


class JsonRpc(pyjsonrpc.JsonRpc):

    @pyjsonrpc.rpcmethod
    def add(self, a, b):
        """Test method"""
        return a + b


# 1. Initialize JSON-RPC class
rpc = JsonRpc()

# 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

CherryPy Example

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

import cherrypy
from pyjsonrpc.cp import CherryPyJsonRpc, rpcmethod


class Root(CherryPyJsonRpc):

    @rpcmethod
    def add(self, a, b):
        """Test method"""
        return a + b

    index = CherryPyJsonRpc.request_handler


print "Starting HTTP server ..."
print "URL: http://localhost:8080"
cherrypy.quickstart(Root())

Licenses

  • GNU Library or Lesser General Public License (LGPL)

  • MIT License

Release files for python-jsonrpc 0.10.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-jsonrpc 0.10.0
File Size Uploaded
python-jsonrpc-0.10.0.tar.gz 21.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-jsonrpc 0.10.0
File Interpreter ABI Platform
python_jsonrpc-0.10.0-py2-none-any.whl Python 2 none any Details

Total release size: 40.0 kB

Release files / python-jsonrpc-0.10.0.tar.gz

Download URL python-jsonrpc-0.10.0.tar.gz
Size 21.2 kB
Tags Source
SHA-256 checksum
How to use checksums
018b9947ca2c97e24a94c7b3b1be8c450cd89fe76cf3dc6bcc9c0fe17f0a3fcb
BLAKE2b-256 checksum
How to use checksums
94e724a062667e4e585529edacdac5fd05ae28d6a67491e737cca7fdfb7a6d17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / python_jsonrpc-0.10.0-py2-none-any.whl

Download URL python_jsonrpc-0.10.0-py2-none-any.whl
Size 18.8 kB
Tags Python 2
SHA-256 checksum
How to use checksums
9f3cf64618c1c31f2aa8afeaa4e87136e82a35e75b52ba18eed829ebae5e0ab4
BLAKE2b-256 checksum
How to use checksums
b0b2946d052824728ee1791807665590eb1ec473aa0db628478a12b368b0ec21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.10.0 This release

2 release files

0.9.3

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.7.12

2 release files

0.7.11

2 release files

0.7.10

2 release files

0.7.8

1 release file

0.7.7

1 release file

0.7.6

1 release file

0.7.4

1 release file

0.7.3

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.6

1 release file

0.2.5

0.2.4

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page