Skip to content

pyjamaswithapachemodpythonjsonrpc

http://lkcl.myopenid.com/ edited this page Jun 27, 2010 · 1 revision

How to make jsonrpc calls from a Pyjamas client to a Python service, with Apache mod_python

(Written by Daniel Carvalho)

This example is based in the Pyjamas "jsonrpc" example

Note: I unpacked pyjamas-0.5p1.tgz in /opt and renamed the folder to "pyjamas". So, the jsonrpc example is located in /opt/pyjamas/examples/jsonrpc

The project has the following relevant files:

  • ./JSONRPCExample.py - this is the client program, that will be compiled to javascript in the build process.
  • ./public/services/EchoService.py - this is the service program. It will be copied without modifications in the build process.

Every other file inside ./public will be copied too, including all the json support files.

I created a symbolic link so that the content will be available via my apache web server:

ln -s /opt/pyjamas/examples/jsonrpc/output /var/www/jsonrpc

The link points to the "output" folder, that is where the compilation result will be stored.

In JSONRPCExample.py, in the EchoServicePython definition, I had to modify the url to "services/EchoService.py". This way, the service location will be relative to the client:

class EchoServicePython(JSONProxy):
    def __init__(self):
        JSONProxy.__init__(self, "services/EchoService.py", ["echo",
"reverse", "uppercase", "lowercase"])

The service is implemented in EchoService.py. The original file was writen to work as a cgi, but I want to run it inside mod_python. Because this way I can keep state between calls, and it is faster too!

#!/usr/bin/env python

class Service:
    def echo(self, msg):
        return msg

    def reverse(self, msg):
        return msg[::-1]

    def uppercase(self, msg):
        return msg.upper()

    def lowercase(self, msg):
        return msg.lower()

from jsonrpc.apacheServiceHandler import handler
#lines removed:
#from jsonrpc.cgihandler import handleCGIRequest
#handleCGIRequest(Service())

I also had to create the apache configuration file in /opt/pyjamas/examples/jsonrpc/public/services/.htaccess (note: this file will also be copied to the "output" folder in the build process)

Options FollowSymLinks
PythonPath "sys.path+['/opt/pyjamas/examples/jsonrpc/output/services']"
AddHandler mod_python .py
PythonHandler EchoService::handler
PythonDebug On

This configures the mod_python, to use the "handler" handler in the "EchoService" module. It also configures the python path so that the json support modules will be found (maybe there is a better way to do that...)

I think it is necessary to restart apache at this point.

Build the project, and open it from the webserver. In my case the url is: http://localhost/jsonrpc/JSONRPCExample.html

Click the "Send to python service" button. Hope it works!

Clone this wiki locally