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': '127.0.0.1'})
    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.


EDIT:
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.

6 comments:

  1. Hello;

    I'm very interested on this topic, but i cant find this option in the python interpreter:

    "Launch Python for Android from emulator and click on Import modules. It will list the newly uploaded egg file. Select it and install."

    The interpreter only shows the keyboard... Where can I find that 'import' option??/

    Thanks!!!

    ReplyDelete
  2. @adykto: I have uploaded screen shots from emulator. See if you get these screens. Steps are simple.

    ReplyDelete
  3. CherryPy website did not provide egg anymore. you can run command:

    setup.py bdist_egg

    to generate egg

    ReplyDelete
  4. Hi Sarabjit, I am Amrit from Bhubaneswar.
    I have some questions, if you could answer me. I am a new programmer in the python language.
    am using directly the SL4A application on my android tablet. what i did is I unzipped the cherrypy source code on my /mnt/sdcard/cherrypy directory and now i can see the cherrypy module under the script / directory. Please can you instruct me to use it correctly so that i can write some program and execute it . :)
    Waiting for your reply.
    Amrit Pati

    ReplyDelete
    Replies
    1. If your cherrypy module is listed then major part is done. Now you can write python webapps that uses cherrypy and copy them to /mnt/sdcard/sl4a/scripts directory. Next you just run this scripts from SL4A.

      Delete
  5. hi Sarabjit,
    I am still not able to execute any python script, its giving me error.
    Could you please tell me step by step how to proceed. Including where to put the cherrypy file in the directory ?
    Thanks in advance.
    Amrit

    ReplyDelete