-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
61 lines (44 loc) · 1.39 KB
/
app.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
#!/usr/bin/python3
import json
try:
import psycopg
except ImportError:
import psycopg2 as psycopg
g_conn = None
DBINFO = "dbname=asahistats user=asahistats"
def app(environ, start_response):
global g_conn
form = {}
if environ['REQUEST_METHOD'] != "POST":
start_response("400 Bad Request", [])
yield b"Method not supported"
return
if g_conn is None:
g_conn = psycopg.connect(DBINFO)
try:
cur = g_conn.cursor()
except Exception:
g_conn = psycopg.connect(DBINFO)
cur = g_conn.cursor()
try:
content_length = int(environ['CONTENT_LENGTH'])
if content_length > 131072:
raise Exception("Too long")
qs = environ["wsgi.input"].read(content_length)
data = json.loads(qs)
if "installer" not in data:
raise Exception("Missing key")
cur.execute("INSERT into stats (data) VALUES (%s);", (json.dumps(data),))
g_conn.commit()
cur.close()
start_response("200 OK", [])
yield b"Request OK"
except Exception:
start_response("400 Bad Request", [])
yield b"Error processing request"
raise
if __name__ == "__main__":
from wsgiref.simple_server import make_server
with make_server('', 8000, app) as httpd:
print("Serving HTTP on port 8000...")
httpd.serve_forever()