![]() Pyjamas with CherryPy JSONRPC
Wiki has Moved¶The Pyjamas Wiki is now at http://pyjs.org/wiki JSON-RPC with pyjamas and cherrypy¶The following has been tested with pyjamas 0.6 and cherrypy 3.1. Pyjamas contains an example client in .../examples/jsonrpc. You can easily test this example with the following cherrypy script: import cherrypy The variable PYJSDIR should contain the absolute path to your .../examples/jsonrpc/output directory. Now start this script and open http://127.0.0.1:9000/JSONRPCExample.html in your browser. The Send to Python Service button should work as desired. Lets have a deeper look at this script. The first method jsonrpchdl is later used as a 'before_handler', ie. it called by cherrypy before cherrypy calls our page handler. It reads the json string from the message body and creates the appropriate keyword args used later in our page handler. The line cherrypy.tools.jsonrpchdl = cherrypy.Tool('before_handler',jsonrpchdl) registers the handler within cherrypy. Now follows the published class Root. It contains two exposed methods: default works as a default handler, ie. its called by cherrypy if there is no other exposed method which could handle the requested url. We use this method to deliver the static pages (its just to keep the example small, its of course not intended to be used in real code!). The most interesting method in our case is services, which has both the expose and the jsonrpc decorators. The kwargs array contains our json parameters. This method just uses getattr to forward the request to the right method and returns a json result. The latter methods implement the functions used in the pyjamas client. |