Bottleで学ぶPython

Bottleのダウンロードサイト

https://pypi.python.org/pypi/bottle

実際のソース

# coding=utf-8
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')



#encoding
@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

#cookie
@route('/hello')
def hello():
    #name = request.cookies.name or 'Guest'
    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!"

#ログイン&cookie
@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)