annotate web/index.py @ 334:3b821541657d

hmac
author Matt Johnston <matt@ucc.asn.au>
date Tue, 12 Jun 2012 00:35:23 +0800
parents 298e502fdcd4
children 048143905092
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
3 import binascii
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
4 import hmac
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
5
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 import bottle
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 from bottle import route, request
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
9 import config
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
10 import log
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
11
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 @route('/update', method='post')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 def update():
334
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
14 enc_lines = request.forms.lines
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
15 mac = request.forms.hmac
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
16
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
17 if hmac.new(config.HMAC_KEY, enc_lines).hexdigest() != mac:
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
18 raise HTTPError(code = 403, output = "Bad key")
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
19
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
20 lines = binascii.a2b_base64(enc_lines).split('\n')
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
21
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
22 log.parse(lines)
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
23
Matt Johnston <matt@ucc.asn.au>
parents: 333
diff changeset
24 return "OK"
333
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 @route('/graph.png')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 def graph():
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 pass
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 @route('/')
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 def top():
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return bottle.template('top', urlparams=request.query_string)
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 def main():
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 bottle.debug(True)
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 bottle.run(port=9999, reloader=True)
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 if __name__ == '__main__':
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 main()
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
298e502fdcd4 Add some web server handling
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41