JSON-RPC Server for Python
Introduction
I've built a JSON-RPC server module for Python, called
SimpleJSONRPCServer.py.
It's derived from the SimpleXMLRPCServer module from Python 2.4,
and subclasses the XML-RPC classes, overriding only what is necessary
to create a JSON-compatible version.
The interface is virtually identical to the classes in
SimpleXMLRPCServer,
except that 'XML' is changed to 'JSON'. For example, the module has
a class called CGIJSONRPCRequestHandler instead of CGIXMLRPCRequestHandler, and
SimpleJSONRPCServer instead of SimpleXMLRPCServer, etc.
Demonstration
Below are two files which demonstrate this JSON-RPC
framework running in a CGI environment:
- jsonrpctest.cgi - server-side CGI,
using
SimpleJSONRPCServer
- jsonrpctest.html - client web page,
which invokes this JSON-RPC service via
Dojo's
dojo.rpc.JsonService JSON-RPC client.
(requires javascript enabled in your browser)
Download
Here again are all files involved in the demo:
- Server-side:
-
SimpleJSONRPCServer.py
- the python module which implements the JSON-RPC server
-
json.py - contains a JSON codec, as needed by SimpleJSONRPCServer.py
-
jsonrpctest.cgi - a tiny CGI script which
implements the server side of the demo
- Client-side:
-
dojo_rpc_only.js - a minimal compressed build of
the Dojo framework, which includes the Dojo JSON-RPC client
-
jsonrpctest.html - a small client-side web page
which uses the JSON-RPC service
License
I've modelled SimpleJSONRPCServer heavily from the original
SimpleXMLRPCServer python library module. To avoid complications,
I release it under the same Python license.