JSON-RPC for python
A json-rpc package which implements JSON-RPC.
This implementation is currently in development.
It provides functionality for making JSON-RPC services available through CGI. In addition it provides a mod_python handler for providing services through mod_python which is more perfomant than CGI. There is also some JSON-RPC over sockets implementation.
You will need simplejson installed for the jsonrpc to run.
Download jsonrpc and install it into python's lib/site-packages/.
On Unix/Linux? do the following as root:
$ easy_install simplejson $ svn co http://svn.json-rpc.org/trunk jsonrpc $ cd jsonrpc $ python setup.py install
The idea is that you only need to create a python module containing a service, put it in a directory and the rest is handled by the mod_python handler.
For it to work you need to tell apache to add a python handler for a certain location or directory. It also works in .htaccess files:
<Directory /var/www/services > AddHandler mod_python py PythonHandler jsonrpc.apacheServiceHandler PythonPath "sys.path+['path_to_jsonrpc_implementation']" </Directory>
You may have to set the PythonPath in the code above, so that mod_python can find the hanlder (see the mod_python documentation).
Now, place a python file in the directory /var/www/services/echo.py. If that file is requested the jsonrpc.apacheServiceHandler will import the python module for that file and look for a service inside that module.
You have 3 options to tell the handler what service you want to provide in your module.
You either expose:
- a service object called service
- a function getService() which returns a service object
- a class Service which will be used to create a service object
Example service script /var/www/services/echo.py:
class Service: def echo(self, msg): return msg service = Service()
All methods of the service object are now available through JSON-RPC over HTTP:
To be used to server services in a CGI environment.
Example CGI script
#!/usr/bin/env python class Service: def echo(self, msg): return msg from jsonrpc.cgihandler import handleCGIRequest handleCGIRequest(Service())
TCP service server
To be used to serve JSON-RPC over TCP.
This is under development
class Service: def echo(self, msg): return msg from jsonrpc.socketserver import ThreadedTCPServiceServer ThreadedTCPServiceServer(Service()).serve(('localhost', 5766))