Tuesday, August 30, 2011

Run Cherrypy web server in Android

I was in much need of a way to run Python CGI scripts in Android. Basically I needed a web server capable of running CGI scripts in Android platform but, I found none. Finally I modified my CGI script to suit the Cherrypy web server and it could run on Android.

I'll try to describe the steps here which I had to figure out myself. The information I found by googling was not completely correct as Android platform has changed from the time of the publication. So, I'll mention the version no. for each software I've used to make sure it is not misunderstood later.

  1. First thing we need is to download the SL4A (r4) software in the Android (2.3) emulator. It can be done from your Android browser by going to the SL4A site. Now install the software in emulator.
  2. Then install Python for Android from the same SL4A site. It'll download an apk of version r4.
  3. Launch SL4A application and check that HelloWorld python script is running. It will make sure that your installation is fine.
  4. Now is the time to install Cherrypy library module. It can be found at http://www.cherrypy.org/wiki/CherryPyDownload. I have taken 3.2.0 version. Download the egg for python 2.6.
  5. Now we need to transfer the egg file to Android emulator. Use adb push command to transfer the egg file to /mnt/sdcard/Download.
  6. Launch Python for Android from emulator and click on Import modules. It will list the newly uploaded egg file. Select it and install.
  7. Now we can write a Cherrypy script to be run as CGI. Below is a HelloWorld example taken from Cherrypy tutorial (modified a bit) -

    # Import CherryPy global namespace
    import cherrypy
    class HelloWorld:
        """ Sample request handler class. """
        def index(self, **params):
            # CherryPy will call this method for the root URI ("/") and send
            # its return value to the client.
            for key in params:
            	print key, '=', params[key]
            return "Hello world!"
        # Expose the index method through the web. CherryPy will never
        # publish methods that don't have the exposed attribute set to True.
        index.exposed = True
    # CherryPy always starts with app.root when trying to map request URIs
    # to objects, so we need to mount a request handler root. A request
    # to '/' will be mapped to HelloWorld().index().
    cherrypy.config.update({'server.socket_host': ''})
    cherrypy.config.update({'server.socket_port': 8080})
    cherrypy.quickstart(HelloWorld(), '/')
  8. The script needs to be transferred to /mnt/sdcard/sl4a/scripts directory in emulator. Then you can launch SL4A and tap the script to run. Debug outputs are visible if you run in SL4A console.
  9. From the Android browser, check the URL http://localhost:8080/. It will say "Hello world".
This the web server set we can place python scripts to access Android phone data and other stuff which can be exposed to the outside clients.

Below are the screen shots from the emulator -

1. Apps list after installing SL4A and Python for Android

2. Tap on Python for Android icon to launch below screen.

3. Tap on Import Modules after copying the CherryPy egg in /mnt/sdcard/Download.

Now tap on CherryPy to install it.