-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwebserver.py
84 lines (75 loc) · 2.08 KB
/
webserver.py
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
import web
import re
import os
import base64
import db
import json
urls = ('/', 'Index', '/logs/(.*)/(.*)', 'Logs', '/login', 'Login', '/operation/(.*)', 'Operations')
html = web.template.render('webtemplates')
logs = web.template.render('cache')
users = (
('admin','admin'),
)
class Index:
def GET(self):
auth = web.ctx.env.get('HTTP_AUTHORIZATION')
if auth is None or validateUser(auth) is False:
web.seeother('/login')
else:
temp = os.popen("vcgencmd measure_temp").readline().replace('temp=', '')
return html.index(temp)
class Logs:
def GET(self, logcat, since):
auth = web.ctx.env.get('HTTP_AUTHORIZATION')
if auth is None or validateUser(auth) is False:
web.seeother('/login')
elif(logcat == 'pump'):
if (since == 'all'):
return logs.pumplog()
else:
return json.dumps(db.getRecentPumpRuns())
elif(logcat == 'thermal'):
if (since == 'all'):
return logs.fanlog()
else:
return json.dumps(db.getRecentFanRuns())
elif(logcat == 'charge'):
if (since == 'all'):
return logs.chargelog()
else:
return json.dumps(db.getRecentChargeRuns())
class Login:
def GET(self):
auth = web.ctx.env.get('HTTP_AUTHORIZATION')
authreq = False
if auth is None:
authreq = True
else:
if validateUser(auth):
raise web.seeother('/')
else:
authreq = True
if authreq:
web.header('WWW-Authenticate','Basic realm="WaterPI"')
web.ctx.status = '401 Unauthorized'
return
class Operations:
def GET(self, operation):
auth = web.ctx.env.get('HTTP_AUTHORIZATION')
if auth is None or validateUser(auth) is False:
web.seeother('/login')
elif(operation == 'runpump'):
os.popen("python3 pumpcontroller.py runnow &")
return "Success"
def validateUser(auth):
auth = re.sub('^Basic ','',auth)
username,password = base64.decodestring(auth).split(':')
if (username, password) in users:
return True
else:
return False
def startServer():
app = web.application(urls, globals())
app.run()
if __name__ == "__main__":
startServer()