good working version
[iankelling.org] / _site / on2vote / vote.py
1 #!/usr/bin/env python2
2 # -*- coding: UTF-8 -*-
3
4 import os
5 import cgi
6 # enable error logging
7 import cgitb
8 import json
9 import socket
10 cgitb.enable(display=0, logdir="../../logs")
11
12 # https://docs.python.org/2/library/cgi.html
13 form = cgi.FieldStorage()
14 # parse the form data into a dictionary
15 vote = json.loads(form.value)
16
17 dir = '/a/www-data/on2votes/' + socket.gethostname()
18 if not os.path.exists(dir):
19 os.makedirs(dir)
20
21 # ips are semi-pii, so we keep this data saved away, and
22 # from time to time, throw away data with too much
23 # duplicate ips, then throw away all the ip data.
24 vote['ip'] = os.environ['REMOTE_ADDR']
25 with open(dir + '/on2votes.log', 'a') as f:
26 json.dump(vote,f)
27 f.write("\n")
28
29 # not that it matters, but tell the client we are happy.
30 print "Status: 200 OK\n";
31 # to read back:
32 #!/usr/bin/env python
33 # import json
34 # with open('../logs/vote.txt', 'r') as f:
35 # for line in f:
36 # print json.loads(line)