This repository was archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
49 lines (37 loc) · 1.37 KB
/
handler.py
File metadata and controls
49 lines (37 loc) · 1.37 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
import http.server
import url_parser
import db
USAGE = "Usage: http://db.sead.systems:8080/(device id)['?' + '&'.join(.[[start_time=(start time as UTC unix timestamp)], [end_time=(end time as UTC unix timestamp)], [type=(Sensor type code)], [subset=(subsample result down to this many rows)], [limit=(truncate result to this many rows)], [json=(1 get the result in pseudo JSON format)]]"
class ApiHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, req, client_addr,server):
http.server.SimpleHTTPRequestHandler.__init__(self, req, client_addr, server)
def do_GET(self):
try:
parsed = url_parser.parse(self.path)
except Exception as inst:
if self.path == '/':
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(USAGE.encode("utf-8"))
self.wfile.flush()
else:
print(type(inst))
print(inst.args)
print(inst)
self.send_error(404)
return
try:
r = db.query(parsed)
self.send_response(200)
self.send_header("Content-type", "application/json;charset=utf-8") # Not actually using JSON format. Causes errors in Chrome when it tries to validate
self.end_headers()
for line in r:
self.wfile.write(line.encode("utf-8"))
except Exception as inst:
self.send_error(500)
print(type(inst))
print(inst.args)
print(inst)
return
self.wfile.flush()