Easily create a CLI and JSON-RPC interface from a common API definition.

The created CLI/RPC interface behaves as you would expect from a python class.

# Returned rpc/cli output (must be JSON serializable).

# Help text taken from method doc string.

# Command name and args taken from method.

# Programm help text taken from class doc string.

# Programm name taken from class name.

# rpc exceptions will also include module version if persent

# automatically added verison command will use module version if present

I simple example application with an add command.

CLI arguments must be given as json data.The json data automatically is unmarshalled before calling the command function and the returned result is automatically marshalled.

Showing the generated help.

Client side json-rpc usage with python-jsonrpc.

Starting the jsonrpc server from the command line.

$ python examples/basic.py startserver Starting Calculator json-rpc service at http://localhost:8080

RPC arguments must be given as json serializable data. The arguments will automatically be marshalled and unmarshalled.

>>> import pyjsonrpc >>> rpc = pyjsonrpc . HttpClient ( url = "http://localhost:8080" ) >>> rpc . add ( 1 , 2 ) 3 >>> rpc . add ( 1.1 , 2.2 ) 3.3000000000000003 >>> rpc . add ( "foo" , "bar" ) u 'foobar' >>> rpc . add ([ 1 , 2 , 3 ], [ 4 , 5 , 6 ]) [ 1 , 2 , 3 , 4 , 5 , 6 ]

Client side exception handeling.

# from examples/exceptions.py import json import pyjsonrpc rpc = pyjsonrpc . HttpClient ( url = "http://localhost:8080" ) try : print rpc . add ( 1 , "str" ) except pyjsonrpc . rpcerror . JsonRpcError as e : print e . code # see http://www.jsonrpc.org/specification#error_object # Server error if an exception is raised during the call. if e . code <= - 32000 and e . code >= - 32099 : print e . message # source exception message data = json . loads ( e . data ) print data [ "classname" ] # source exception class name print data [ "repr" ] # source exception repr string print data [ "traceback" ] # source exception traceback print data [ "version" ] # source module version if present

Starting the jsonrpc service from within python.

import basic api = basic . Calculator () api . startserver ()

Getting a pyjsonrpc.HttpRequestHandler for further use.