rework server to use flask, new items

This commit is contained in:
Pavol Rusnak 2011-06-12 23:10:22 +02:00
parent 99893f58a0
commit bd828a71e8
8 changed files with 84 additions and 35 deletions

View file

@ -1,41 +1,28 @@
#!/usr/bin/env python
#!/usr/bin/python
import BaseHTTPServer
from flask import Flask, render_template
class BrmbarHandler(BaseHTTPServer.BaseHTTPRequestHandler):
app = Flask('BrmBar')
def do_GET(self):
action = self.path.split('/')[1:]
@app.route('/')
def index():
return render_template('index.html')
if action[0] == 'code':
self.code(action[1])
return
@app.route('/code/<code>')
def code(code):
return 'CODE "%s" received' % code
if action[0] == 'favicon.ico':
self.send_file('favicon.png', 'image/png')
return
@app.route('/admin/')
def admin():
return render_template('admin.html')
self.send_response(404)
@app.route('/log/')
def log():
return render_template('log.html')
def code(self, code):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write('CODE "%s" received\n' % code)
@app.route('/stats/')
def stats():
return render_template('stats.html')
def send_file(self, filename, mimetype):
self.send_response(200)
self.send_header('Content-type', mimetype)
self.end_headers()
f = open(filename, 'r')
self.wfile.write(f.read())
f.close()
try:
server_address = ('', 8000)
httpd = BaseHTTPServer.HTTPServer(server_address, BrmbarHandler)
print 'Starting BrmBar server ...'
httpd.serve_forever()
except KeyboardInterrupt:
print 'Shutting down ...'
httpd.socket.close()
if __name__ == '__main__':
app.run()