# Copyright 2011 Textler, Inc # Version 0.92 jkelkar Sun Jul 17 09:35:25 2011 import string ,cgi, time from os import curdir, sep from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer debug = 0 live = 1 if live: import android import time import traceback import os, urllib, urllib2, base64 def sendFile(f, fn, h): try: filedata = open(fn).read() outdata = base64.b64encode(filedata) if debug: print 'sending %s bytes' % len(outdata) data = {'filename': f, 'fdata':outdata} params = urllib.urlencode(data) if h[:4] == 'demo': app = 'demo' else: app = 'wcp' url = "https://%(h)s/%(app)s/photo/filer" % vars() print 'Sending to %s' % url if debug: print url try: response = urllib.urlopen(url, params) the_page = response.read() except: print 'Failed to connect to website.' raise IOError # traceback.print_exc() except: print 'Could not find file: %s' % fn def get_data(names, querystr): out = {} q = querystr.split('&') qx = {} for qeach in q: qa, qb = qeach.split('=') qx[qa] = qb if type(names) == type([]): for name in names: if qx.has_key(name): out[name] = qx[name] else: out[name] = '' elif type(name) == type('string'): if qx.has_key(name): out[name] = qx[name] # print out return out class MyHandler(BaseHTTPRequestHandler): def do_GET(self): fn = '' try: if self.path[:6] == '/filer': if debug: print self.path[7:] rfg = get_data(['p', 'fs', 'w', 'h'], self.path[7:] ) p = rfg['p'] fs = rfg['fs'] w = rfg['w'] h = rfg['h'] if debug: print 'p: %(p)s, fs: %(fs)s, w: %(w)s, h: %(h)s' % vars() dirname = '/sdcard/DCIM/Camera/' fname = 'p%s_%s_w%s_%s.jpg' % (p, fs, w, time.strftime('%y%m%d_%H%M%S')) filename = dirname+fname if debug: print 'Sending to file:', filename if live: droid = android.Android() seen = 0 while not seen and not os.path.exists(filename): ct = 4 droid.cameraInteractiveCapturePicture(filename) while ct > 0: time.sleep(1) if not os.path.exists(filename): print 'Waiting ...' ct -= 1 else: print 'Picture present' seen = 1 ct = 0 if not seen: print 'Done with all tries, pic not found - taking picture again' if debug == 2: print os.stat(filename) for f in os.listdir(dirname): print f os.system('ls -l %s%s' % (dirname, f)) print 'Sending file:', filename sendFile(fname, filename, h) print 'file %s sent' % filename os.unlink(filename) print 'File removed' else: pass # traceback.print_exc() return except IOError: self.send_error(404,'File Not Found: %s' % self.path) def main(): try: server = HTTPServer(('localhost', 9000), MyHandler) print 'httpserver started ...' server.serve_forever() except KeyboardInterrupt: print '^C received, shutting down server' server.socket.close() if __name__ == '__main__': main()