1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#!/usr/bin/python -u
from __future__ import absolute_import
from __future__ import print_function
#
# droiddemo.py, Copyright 2010-2013, The Beanstalks Project ehf.
# http://beanstalks-project.net/
#
# This is a proof-of-concept PageKite enabled HTTP server for Android.
# It has been developed and tested in the SL4A Python environment.
#
DOMAIN='phone.bre.pagekite.me'
SECRET='ba4e5430'
SOURCE='/sdcard/sl4a/scripts/droiddemo.py'
#
#############################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
#
import six
from six.moves.urllib import unquote
from six.moves.urllib.parse import urlparse
import android
import pagekite
import os
class UiRequestHandler(pagekite.UiRequestHandler):
CAMERA_PATH = '/sdcard/dcim/.thumbnails'
HOME = ('<html><head>\n'
'<script type=text/javascript>'
'lastImage = "";'
'function getImage() {'
'xhr = new XMLHttpRequest();'
'xhr.open("GET", "/latest-image.txt", true);'
'xhr.onreadystatechange = function() {'
'if (xhr.readyState == 4) {'
'if (xhr.responseText && xhr.responseText != lastImage) {'
'document.getElementById("i").src = lastImage = xhr.responseText;'
'}'
'setTimeout("getImage()", 2000);'
'}'
'};'
'xhr.send(null);'
'}'
'</script>\n'
'</head><body onLoad="getImage();" style="text-align: center;">\n'
'<h1>Android photos!</h1>\n'
'<img id=i height=80% src="http://www.android.com/images/opensourceproject.gif">\n'
'<br><a href="/droiddemo.py">source code</a>'
'| <a href="/status.html">kite status</a>\n'
'</body></head>')
def listFiles(self):
mtimes = {}
for item in os.listdir(self.CAMERA_PATH):
iname = '%s/%s' % (self.CAMERA_PATH, item)
if iname.endswith('.jpg'):
mtimes[iname] = os.path.getmtime(iname)
files = list(six.iterkeys(mtimes))
files.sort(key=lambda iname: mtimes[iname])
return files
def do_GET(self):
(scheme, netloc, path, params, query, frag) = urlparse(self.path)
p = unquote(path)
if p.endswith('.jpg') and p.startswith(self.CAMERA_PATH) and ('..' not in p):
try:
jpgfile = open(p)
self.send_response(200)
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', '%s' % os.path.getsize(p))
self.send_header('Cache-Control', 'max-age: 36000')
self.send_header('Expires', 'Sat, 1 Jan 2011 12:00:00 GMT')
self.send_header('Last-Modified', 'Wed, 1 Sep 2011 12:00:00 GMT')
self.end_headers()
data = jpgfile.read()
while data:
try:
sent = self.wfile.write(data[0:15000])
data = data[15000:]
except Exception:
pass
return
except Exception as e:
print('%s' % e)
pass
if path == '/latest-image.txt':
flist = self.listFiles()
self.begin_headers(200, 'text/plain')
self.end_headers()
self.wfile.write(flist[-1])
return
elif path == '/droiddemo.py':
try:
pyfile = open(SOURCE)
self.begin_headers(200, 'text/plain')
self.end_headers()
self.wfile.write(pyfile.read().replace(SECRET, 'mysecret'))
except IOError as e:
self.begin_headers(404, 'text/plain')
self.end_headers()
self.wfile.write('Could not read %s: %s' % (SOURCE, e))
return
elif path == '/':
self.begin_headers(200, 'text/html')
self.end_headers()
self.wfile.write(self.HOME)
return
return pagekite.UiRequestHandler.do_GET(self)
class DroidKite(pagekite.PageKite):
def __init__(self, droid):
pagekite.PageKite.__init__(self)
self.droid = droid
self.ui_request_handler = UiRequestHandler
def Start(host, secret):
ds = DroidKite(android.Android())
ds.Configure(['--defaults',
'--httpd=localhost:9999',
'--backend=http:%s:localhost:9999:%s' % (host, secret)])
ds.Start()
Start(DOMAIN, SECRET)
|