Search code examples
androidpythonwebservercgisl4a

Running an Android phone as a stable webserver (for a Python CGI script)


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?


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.