-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdash_server.py
More file actions
67 lines (55 loc) · 1.48 KB
/
dash_server.py
File metadata and controls
67 lines (55 loc) · 1.48 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
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
# enable debugging
import os
import sys
import socket
import pwd
import cgi
import cgitb
import urlparse
from pprint import pprint
from time import strftime, localtime, time
# dash.py?press=stove
# dash.py?last=stove
#
cgitb.enable(format='text')
print "Content-Type: text/plain;charset=utf-8"
print
data_path = '/home/alanb0/public_html/data/dash/'
max_records = 10 # not implemented
def main():
req_time = time()
env = os.environ
url = env['REQUEST_URI']
params = urlparse.parse_qs(urlparse.urlparse(url).query)
if 'press' in params:
button = params['press'][0]
handle_button_press(env, button, req_time)
elif 'last' in params:
button = params['last'][0]
get_last_record(env, button, req_time)
else:
print('?')
# todo: handle different types of display requests
# - total count
# - count in time period
# - etc
def handle_button_press(env, button, req_time):
addr = env['REMOTE_ADDR']
port = env['REMOTE_PORT']
fname = data_path + button + '.txt'
try:
with open(fname, 'a') as f:
f.write('%s %s %s\n' % (req_time, addr, port))
except Exception as e:
print('error %s' % e)
else:
print('success %s' % req_time)
def get_last_record(env, button, req_time):
fname = data_path + button + '.txt'
with open(fname, 'r') as f:
for line in f:
pass
print(line.split(' ')[0])
if __name__=='__main__':
main()