実際のソース
from bottle import get, post, route, run, static_file, response, abort, redirect, request, template, os
@route('/images/<filename:re:.*\.png>')
def send_image(filename):
return static_file(filename, root='/Users/chuno/Documents', mimetype='image/png');
@route('/images/<filename>')
def send_image(filename):
return static_file(filename, root='/Users/chuno/Documents');
@route('/wiki/<pagename>')
def show_wiki_page(pagename):
return template('Hello{{name}}, how are you?', name=pagename)
@route('/static/<filename>')
def server_static(filename):
return static_file(filename, root='/Users/chuno/Documents')
@route('/iso')
def get_iso():
response.character = 'ISO-8859-15'
return u'This will be sent with ISO-8859-15 encoding'
@route('/latin9')
def get_latin():
response.cotent_type = 'text/html; charset=latin9'
return u'ISO-8859-15 is also known as latin9'
@route('/download/<filename:path>')
def download(filename):
return static_file(filename, root='/Users/chuno/Documents', download=filename)
@route('/restricted')
def restricted():
abort(401, "Sorry, your access denied!")
@route('/wrong/url')
def wrong():
redirect("/iso")
@route('chuno/<page>')
def chuno(page):
response.set_header('Content-Language', 'ja')
if page == 'chuno':
redirect("/iso")
else:
return False
@route('/hello')
def hello():
name = request.get_cookie("account", secret='some-secret-key')
return template("Hello {{name}}", name=name)
@route('/hello/<name>')
def hello_again(name):
if request.get_cookie("visited"):
return "Welcome back!" + name
else:
response.set_cookie("visited", "yes")
return "Hello, there! Nice to meet you!"
@get('/login')
def login():
return '''
<form action="/login" method="post">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input value="Login" type="submit" />
</form>
'''
@route('/login', method='POST')
def do_login():
username = request.forms.get('username')
password = request.forms.get('password')
if check_login(username, password):
response.set_cookie("account", username, secret='some-secret-key')
return template("<p>Wellcome! {{name}} Your login information was correct.</p>", name=username)
else:
return "<p>Login failed.</p>"
def check_login(username, password):
if username == 'chuno' and password=='111':
return True
else:
return False
@route('/restricted')
def restricted_area():
username = request.get_cookie("account", secret='some-secret-key')
if username:
return template("Hello {{name}}! Welcome back.", name=username)
else:
return "You are not logged in. Access denied."
@get('/upload')
def upload():
return '''
<form action="/upload" method="post" enctype="multipart/form-data">
Category: <input type="text" name="category">
Select a file: <input type="file" name="upload">
<input type="submit" value="start upload">
</form>
'''
@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.png', '.jpg', '.jpeg'):
return 'File extension not allowed.'
save_path = get_save_path_for_category(category)
upload.save(save_path)
return 'OK'
def get_save_path_for_category(category):
path = '/Users/chuno/Documents'
return path
run(host='localhost', port=8080, debug=True)