5

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?

Community
  • 1
  • 1
ShankarG
  • 1,105
  • 11
  • 26

4 Answers4

8

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.

Carl Smith
  • 3,025
  • 24
  • 36
  • Thanks - first answer to really address the question, if only partially, so I'm giving you the bounty anyway :). In fact the system at present runs with a laptop as the server and a normal phone (i.e. not an Android or smartphone) that handles the SMS interface and connects to the computer via Gammu(www.wammu.eu). But the problem arises because laptop batteries deteriorate over continuous running, because the phone connection is a little unstable, and because I have to rely on my landline broadband for the webserver, which is also unreliable. Mobile data should hopefully work better. – ShankarG Sep 23 '13 at 06:03
  • I hope wakelocks would be the solution to keeping the phone awake (I think that's what they're for?). Power cuts generlaly only last about 2 hours at most so it should not discharge fully in that time. Also, do you have any idea if receiving an SMS will wake up the phone? – ShankarG Sep 23 '13 at 06:05
  • Wakelocks should be your friend, but I've no experience with them. Regarding waking the phone by SMS, I have no idea. I was going to test it, but then thought it may be device specific, and I just don't have the resources to test that. I can only suggest putting your device to sleep, then texting it and seeing what happens. – Carl Smith Sep 24 '13 at 15:19
  • Apologies, I just realised I hadn't given you the bounty, so now I have :). There are SMS gateways that function on Android phones so I presume this functionality is available, at least if a partial wakelock is acquired. I am currently testing the impact on battery life by just acquiring a partial wakelock for a few hours and then seeing what percentage of battery is used as a result. – ShankarG Sep 25 '13 at 10:58
  • Thank you, and all the best with it. If you'd like to post any interesting findings here as another answer, that'd be cool. Cheers. – Carl Smith Sep 25 '13 at 21:42
3

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:

Community
  • 1
  • 1
LOG_TAG
  • 19,894
  • 12
  • 72
  • 105
  • thanks for the tip, but my question was about the stability of such a system since it's alpha quality software. do you have any experience with that? Also micro httpd and groomdroid claim to run cgi, is that not true? – ShankarG Sep 18 '13 at 14:45
  • I have tried with raspberry pi for my friend academic project model! at that I have done some hacks with mobile. node.js is better I guess! – LOG_TAG Sep 18 '13 at 17:16
  • 1
    I'm aware of the Raspberry Pi and in fact this system was already running on an ARM-based plug computer for a year. But none of these systems works out well because I live in an area with frequent power cuts and we have no money whatsoever to arrange battery backup for them. This has been a major problem. Hence the idea of using a phone. – ShankarG Sep 19 '13 at 09:19
  • 1
    http://code.google.com/p/i-jetty/ jetty for java web-server on Android, for power-cuts at your region only **modi** fication is the solution!:) – LOG_TAG Sep 19 '13 at 09:44
  • You've provided a lot of detail, but it has nothing to do with the question, and is incorrect anyway. – Carl Smith Sep 23 '13 at 02:13
1

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/

Mat Baker
  • 134
  • 5
  • Thanks for this tip, but the main focus of the question is not on whether this is possible - I guessed that it was - but how stable the resulting set up is. How stable has your system been? Have you been able to run it 24/7 for some significant time period? That's what I'd like to check. – ShankarG Sep 21 '13 at 16:47
  • 1
    Perhaps the most unreliable part for me seemed to be my service provider. Also something to consider here would be Fast Dormancy (http://androidforums.com/samsung-galaxy-s2-international/674208-fast-dormancy-in-nutshell.html). Phones including my nexus 4 often switch between UMTS and HSPA to prolong battery life. Good thing is seeing as though the aforementioned approach is so easy to get up and running you could whip something up and do some testing with little to no impediments. – Mat Baker Sep 21 '13 at 21:51
0

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.

gavinb
  • 19,278
  • 3
  • 45
  • 60
  • The current system goes very much on your lines, but that's what I need to change :). It used to be a plug computer linked to a phone with gammu, now a laptop. These are the problems: power cuts make plug computer / "old PC" unsuitable; phone connection over bluetooth dies periodically; bluetooth dongle itself dies periodically; laptop suffers occasional kernel crashes; landline internet is not reliable; etc. In sum the system has too many points of failure. The fully cloud based solution does not work because Twilio-style SMS gateways here (India) are too expensive for us. – ShankarG Sep 24 '13 at 17:46
  • Currently I'm actually thinking of just running an Android phone as an SMS gateway and having the rest of the infrastructure in the cloud (AWS free tier, for the first year at least). Perhaps that would be the best of all worlds. The phone would still need to be awake enough to handle receiving / sending SMS though. – ShankarG Sep 24 '13 at 17:49
  • This Quora post seems to have some good info on SMS services in India: http://www.quora.com/SMS-Gateways/What-are-Indias-Best-SMS-Gateway-providers Twilio is just 1c/SMS outbound, but doesn't currently support inbound. – gavinb Sep 25 '13 at 12:52
  • I still think you're better off avoiding using a phone at all. It's just not suited to a server role. Running in the cloud avoids most of your problems listed above. Hopefully you can find an affordable SMS gateway. – gavinb Sep 25 '13 at 12:53