annotate web/index.py @ 28:e3e0ed7758f9

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