Question

I'm new to Android. I have a Python program that is both a CGI script as well as an SMS-based interaction system for a small database. It is an extremely low demand system (a handful of users) being run by a grassroots organisation. But it requires stability, in the sense of not having random crashes or down time. For various reasons, running this on an Android phone would resolve some problems with the existing setup. However, before I dive in, I wanted to check regarding the feasibility of such a system. It would have to:

  • Run a web server that could execute CGI scripts (vanilla CGI)
  • Respond to SMSs
  • Handle an SQlite database
  • Do so in Python (as porting it to Java is not feasible due to time constraints)

Interfacing with phone users is not required at this stage.

I am aware that the pieces that would be needed exist - web servers with CGI, SL4A, etc. But the webservers mostly seem intended for personal use and SL4A clearly states that it "is alpha quality software". The various questions on SO relating to SL4A also don't seem to say much on this kind of use case. They are focused on application development for phones (such as this one).

In short, would such a system be stable? Assuming the core program is sound, could I rely on it?

Was it helpful?

Solution

TLDR: CherryPy is a dependable server, and Android may be dependable enough to build servers on these days.


I used to maintain a project that used CherryPy and SL4A (with ws4py for websockets).

CherryPy 3.2.2 worked perfectly on Python 2.6 and Python 3.2.

The application was often running for a day or two. It seemed like it would have been fine if it ran longer, but it got restarted a lot as it was being developed.

CherryPy was always fine, but Android devices do sometimes just crash, so SL4A will exit from time to time, and need to be restarted. This may not be an issue on a device that was only used as a server. For me, it was always stable when the device was left running overnight, but would occasionally crash when I was using the device normally (it was my actual phone). All of this was on a Galaxy SII, back when Android was still pretty buggy like that.

Setting up CherryPy is easy. It's pure Python, so you can just drop a copy onto your path some place and import it (you do not need an emulator either).

You may struggle to keep the device awake. If it is left alone, it will go to sleep. You can tell it to stay awake in the developer options, but I am pretty sure that only works if the device is charging.

UPDATE: Android is much more stable now days, but work on the SL4A project has pretty much ended. There's a project called QPython that maintains SL4A as part of a bigger app, so this stuff can still be done.

OTHER TIPS

For running Python CGI scripts in Android. Basically you needed a web server capable of running CGI scripts in Android platform but, I found none. You need toy CGI script to suit the Cherrypy web server and it could run on Android.

Are steps are here :

  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.

Credits to: see the screen shots here

Other Info:

micro-httpd (GET / POST / CGI support) cross-compiled to Android

python-for-android

Running "Hello, world!" as a CGI Script

What I recommend:

I suggest to go for any lightweight web servers (like node.js) in raspberry pi

Ref:

Here is a link to one of my blog posts describing how to turn your android phone in a basic CGI webserver using Py4A / SL4A in around 10 minutes http://matbaker.wordpress.com/2013/01/29/android-webserver-in-10-minutes/

Your application requires running a server, which presumes constant availability, connectivity and unrestricted processing power. A mobile phone provides none of these.

Mobile phones are designed to conserve battery life by minimising processing and power demands. These two points are directly at odds with running a server.

But it requires stability, in the sense of not having random crashes or down time.

Assuming your software is correctly written, you may not have to worry about crashes, but the OS will be constantly trying to put the system to sleep.

  • Run a web server that could execute CGI scripts (vanilla CGI)
  • Respond to SMSs
  • Handle an SQlite database
  • Do so in Python (as porting it to Java is not feasible due to time constraints)

Why not just use a small virtual server? You can easily purchase a server for as little as $5 a month (e.g. DigitalOcean) and use Twilio as an SMS gateway (a few cents per SMS). Cheap, available, reliable and highly available. You get a preinstalled Ubuntu image, install a few extra packages on there and away you go with Python and SQLite. And you don't have to worry at all about the mobile/SMS side of things (signal strength, network availability, etc). All these points above would be satisfied by this solution.

If you don't want to operate a server externally (ie. "in the cloud") then you could use an old PC and install Linux. Or even use a plug computer and have an almost invisible server. There are so many hardware options that are more suitable than a mobile phone.

For various reasons, running this on an Android phone would resolve some problems with the existing setup.

How? What is it about Android that makes it preferable? If you're referring to the laptop battery problem, the phone would suffer a similar (if not worse) fate.

In short, would such a system be stable? Assuming the core program is sound, could I rely on it?

You will probably be fighting the OS and the hardware the whole time. A server wants to be running constantly, a mobile phone wants to be running as little as possible. The entire stack of hardware and software is optimised for power conservation. A mobile is designed to run from a battery with intermittent charging, and its thermal management systems are designed to wake the CPU as infrequently as possible. If you try to run a mobile as a server, you will likely wear out the hardware very quickly. They are simply not designed to run 24/7. You could run such a system, and it would probably work for a while, but I expect you would have to replace the hardware before too long.

Use hardware suited to the task of running a server. Old PCs are so easy to come by these days, it should be possible to get one for free that would be up to the task.

Also, is CGI really a requirement? You should really look at WSGI; it is far more efficient. CherryPi is a nice Python server, and Flask is definitely worth a look too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top