-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetupServer.py
More file actions
55 lines (49 loc) · 1.59 KB
/
setupServer.py
File metadata and controls
55 lines (49 loc) · 1.59 KB
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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import ConfigParser
import json
import requests
import socket
import urlparse
from string import Template
ID_KEY = 'chairid'
APP_LINK = 'http://www.android.com'
PAGE_TEMPLATE = Template(Template('''<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta name="chairid" content="$chairid">
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=$app_link">
<script type="text/javascript">
window.location.href = "$app_link"
</script>
<title>PECS Chair Setup</title>
</head>
<body>
If you are not redirected automatically, follow the <a href='$app_link'>link to download app</a>
</body>
</html>
''').safe_substitute(app_link=APP_LINK))
def generate_page(chair_id):
return PAGE_TEMPLATE.safe_substitute(chairid=chair_id)
class InitializationHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
path, tmp = self.path.split('?', 1)
qs = urlparse.parse_qs(tmp)
chair_id = qs[ID_KEY]
chair_id = int(chair_id[0])
except:
print "sending 400: invalid"
self.send_response(400)
return
if ID_KEY in qs:
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(generate_page(chair_id))
else:
print "sending 400: missing"
self.send_response(400)
return
serv = HTTPServer(('', 38002), InitializationHandler)
serv.serve_forever()