CherryPy installation on Android

2012年10月4日

Background: I am using Jack Palevich’s Android Terminal (Thanks a lot to Jack!), with busybox, vim (a powerful text editor suitable for both general-purpose editing and programming) and SL4A (containing Python 2.6.7) installed. I configured Android Terminal to make the startup Android standard shell launch the busybox shell automatically. I have configured quite a few symbolic links and shell scripts to busybox so that I can use the “cp”, “rm” and “ll” easily. My phone is not rooted because I’m a little bit worried about warranty; on the other hand, it means these steps apply to almost all non-rooted Android phones or tablets.

In order to install CherryPy on Android, I’ve referred to Sylvain’s blog post at http://www.defuze.org/archives/228-running-cherrypy-on-android-with-sl4a.html. Based on this, I began my journey.

The first thing is of course to extract CherryPy. If you use the busybox unzip applet and extract the files to the sdcard, it would report unable to set permissions on files. However, it’s preferrable to extract it to somewhere in the phone’s internal flash memory. Fortunately CherryPy is less than 2MB during all the installation steps, so that it won’t fill up my phone’s limited internal memory, which is only 512MB.

I extracted the CherryPy directory to /data/data/jackpal.androidterm/shared_prefs/kbox/robbie/CherryPy. This is because I’m running inside Android Terminal so it can extract to only the data directory for Android Terminal. Then, according the Sylvain’s blog, I ran “python setup.py build”. Afterwards, I got the build result in the CherryPy/build directory.

Then, I copied build/cherryd to /data/data/jackpal.androidterm/shared_prefs/kbox/bin, which is listed in my PATH environment variable. However, I found cherryd must be modified to make it run in SL4A. I ran vim on it, editing the first line in it:

#!/data/data/jackpal.androidterm/shared_prefs/kbox/bin/python

This is because previous running of setup.py references the python binary executable, which is at /data/data/com.googlecode.pythonforandroid/files/python/bin/python. This binary file doesn’t automatically load the SL4A environment (such as dynamically-linked libraries–“*.so” files). If you run the original cherryd you will see error messages from python. My /data/data/jackpal.androidterm/shared_prefs/kbox/bin/python script contains content similar to https://code.google.com/p/python-for-android/source/browse/python-build/standalone_python.sh.

The next step is to copy all the files in build/lib/cherrypy to /sdcard/com.googlecode.pythonforandroid/extras/python/cherrypy.

Then, you can try running cherryd. It should work. Use Ctrl+C to terminate it.

Then, go to cherrypy/tutorial, and run python tut01_helloworld.py. Go to your browser, open http://localhost:8080. You will see “Hello World!” in a page. It works!

After the initial test, don’t forget to delete CherryPy directory to free it out of the precious internal flash memory space (unless you don’t care or your phone has a lot of space).

留下您的评论