Some Fun With Android and Python

July 11, 2011

For those who don't know, the Android Scripting Environment allows you to use scripting languages to experiment with Android.

I wanted to try it, so I wrote this little web server that takes pictures when you make an HTTP request to it.

import BaseHTTPServer, android
from os import path

droid = android.Android()
FILENAME = "/sdcard/sl4a/spy/pic.jpg"
PORT = 9090

class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(s):
"""Respond to a GET request."""
droid.cameraCapturePicture(FILENAME)
picture = open(FILENAME, 'rb')
s.send_response(200)
s.send_header("Content-Type", "image/jpeg")
s.end_headers()
s.wfile.write(picture.read())

server_class = BaseHTTPServer.HTTPServer
httpd = server_class(('', PORT), Handler)
httpd.serve_forever()

The code is on Github.