http://article.yeeyan.org/view/35282/120735
http://snipplr.com/view/26459/python-web-with-bottle-and-session-beaker/
that's all enough,enjoy
here is the detail:
from beaker.middleware import SessionMiddleware
import bottle
import solr
import StringIO
import urllib
import re
bottle.debug(True) #remove in production
session_opts={
'session.type':'file',
'session.cookie_expires':300,
'session.data_dir':'./data',
'session.auto':True
}
def login_required(func):
def check_login(*args,**dict):
session=bottle.request.environ["beaker.session"]
if "is_login" in session and session["is_login"]==True:
return func(*args,**dict)
redirect("./login.html")
return check_login
def login():
session=bottle.request.environ["beaker.session"]
session["is_login"]=True
redirect("./")
def logout():
session=bottle.request.environ["beaker.session"]
session["is_login"]=False
redirect("./login.html")
@route("/login.html")
@view("manage/login")
def login_get():
return {}
@route("/login.html",method="POST")
def login_post():
import hashlib
passwd=request.POST.get("pass").strip()
if hashlib.new("md5", passwd).hexdigest()=="xxx":
login()
redirect("./login.html")
@route("/logout.html")
def logout_get():
logout()
usage:
@route("/category/create.html")
@login_required
@view("category/create")
def category_create():
#response.content_type = 'text/html; charset=gb2312'
list=Category.query.all()
return {"list":list}