Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 411
ShankarG Avatar asked Sep 15 '13 05:09

ShankarG


3 Answers

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.

like image 92
Carl Smith Avatar answered Sep 28 '22 17:09

Carl Smith


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:

  • Raspberrry pi python
  • lightweight web servers
  • web application framework for Node.js
like image 20
LOG_TAG Avatar answered Sep 28 '22 16:09

LOG_TAG


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/

like image 34
Mat Baker Avatar answered Sep 28 '22 17:09

Mat Baker