Mercurial > templog
annotate web/templog.py @ 47:340a14fcbaeb
change timeout for readline
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sun, 24 Jun 2012 10:36:12 +0800 |
parents | 9b5b202129c3 |
children | 79761ee67134 |
rev | line source |
---|---|
27 | 1 #!/usr/bin/env python2.7 |
2 | |
28 | 3 import binascii |
4 import hmac | |
29 | 5 import zlib |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
6 import datetime |
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
7 import time |
28 | 8 |
27 | 9 import bottle |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
10 from bottle import route, request, response |
27 | 11 |
28 | 12 import config |
13 import log | |
14 | |
27 | 15 @route('/update', method='post') |
16 def update(): | |
28 | 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 | |
29 | 23 lines = zlib.decompress(binascii.a2b_base64(enc_lines)).split('\n') |
28 | 24 |
25 log.parse(lines) | |
26 | |
27 return "OK" | |
27 | 28 |
29 @route('/graph.png') | |
30 def graph(): | |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
31 # url takes time in hours or days |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
32 if 'day' in request.query: |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
33 start_day = datetime.datetime.strptime(request.query.day, '%Y%m%d') |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
34 start = time.mktime(start_day.timetuple()) |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
35 length = int(request.query.length) * 3600 * 24 |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
36 else: |
31
5e75e08d20ac
- Various fixes for web server, kind of works
Matt Johnston <matt@ucc.asn.au>
parents:
30
diff
changeset
|
37 start_hour = datetime.datetime.strptime(request.query.hour, '%Y%m%d%H') |
30
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
38 start = time.mktime(start_hour.timetuple()) |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
39 length = int(request.query.length) * 3600 |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
40 |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
41 response.set_header('Content-Type', 'image/png') |
13fcf497f8b7
parse the arguments for start/length
Matt Johnston <matt@ucc.asn.au>
parents:
29
diff
changeset
|
42 return log.graph_png(start, length) |
27 | 43 |
44 @route('/') | |
45 def top(): | |
46 return bottle.template('top', urlparams=request.query_string) | |
47 | |
37
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
48 @route('/test') |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
49 def test(): |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
50 import config |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
51 import os |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
52 f = open('%s/testout' % config.DATA_PATH, 'a+') |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
53 f.write("more") |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
54 f.flush() |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
55 f.close() |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
56 |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
57 return 'done' |
8da0fdadc8d7
- Getting there, update has problems
Matt Johnston <matt@ucc.asn.au>
parents:
31
diff
changeset
|
58 |
27 | 59 def main(): |
60 bottle.debug(True) | |
40 | 61 bottle.run(server='cgi') |
62 #bottle.run(port=9999, reloader=True) | |
27 | 63 |
64 if __name__ == '__main__': | |
65 main() | |
66 | |
67 |