Skip to main content

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

Project description

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

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.8.3.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

python_jsonrpc-0.8.3-py2-none-any.whl (17.7 kB view details)

Uploaded Python 2

File details

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

File metadata

File hashes

Hashes for python-jsonrpc-0.8.3.tar.gz
Algorithm Hash digest
SHA256 aff6583d0eeb3cd8f80274796a6e0d6c5052e4713c0cffbaf76c95a364561193
MD5 dc8735e42690f688ae5c7db5a83b67dd
BLAKE2b-256 c1392feee100862ff535f290c64cc92198d4d2e9068d8581ad56d9ddbedf9944

See more details on using hashes here.

File details

Details for the file python_jsonrpc-0.8.3-py2-none-any.whl.

File metadata

File hashes

Hashes for python_jsonrpc-0.8.3-py2-none-any.whl
Algorithm Hash digest
SHA256 d7d1b4e238990ebbfb4e84f2353aa2dcd923bb998b43d6d80b5d930d0d9cc056
MD5 04cfbaaa81a997b2b4ba50b8bfbf0953
BLAKE2b-256 701077b312f0c37f17660403fc9817e469be4386e8adae4448dfb5462a55d6e9

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