comparison web/templog.py @ 37:8da0fdadc8d7

- Getting there, update has problems
author Matt Johnston <matt@ucc.asn.au>
date Sat, 16 Jun 2012 09:08:07 +0800
parents web/index.py@5e75e08d20ac
children 9b5b202129c3
comparison
equal deleted inserted replaced
34:79124d7d3f79 37:8da0fdadc8d7
1 #!/usr/bin/env python2.7
2
3 import binascii
4 import hmac
5 import zlib
6 import datetime
7 import time
8
9 import bottle
10 from bottle import route, request, response
11
12 import config
13 import log
14
15 @route('/update', method='post')
16 def update():
17 enc_lines = request.forms.lines
18 mac = request.forms.hmac
19
20 if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac:
21 raise HTTPError(code = 403, output = "Bad key")
22
23 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n')
24
25 log.parse(lines)
26
27 return "OK"
28
29 @route('/graph.png')
30 def graph():
31 # url takes time in hours or days
32 if 'day' in request.query:
33 start_day = datetime.datetime.strptime(request.query.day, '%Y%m%d')
34 start = time.mktime(start_day.timetuple())
35 length = int(request.query.length) * 3600 * 24
36 else:
37 start_hour = datetime.datetime.strptime(request.query.hour, '%Y%m%d%H')
38 start = time.mktime(start_hour.timetuple())
39 length = int(request.query.length) * 3600
40
41 response.set_header('Content-Type', 'image/png')
42 return log.graph_png(start, length)
43
44 @route('/')
45 def top():
46 return bottle.template('top', urlparams=request.query_string)
47
48 @route('/test')
49 def test():
50 import config
51 import os
52 f = open('%s/testout' % config.DATA_PATH, 'a+')
53 f.write("more")
54 f.flush()
55 f.close()
56
57 return 'done'
58
59 def main():
60 bottle.debug(True)
61 bottle.run(port=9999, reloader=True)
62
63 if __name__ == '__main__':
64 main()
65
66