python web with bottle and session (beaker)

python web with bottle and session (beaker)

http://icodesnip.com/snippet/python/python-web-with-bottle-and-session-beaker
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
       
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    import bottle as web
    from beaker.middleware import SessionMiddleware
    
    @web.route('/')
    def index():
        session=web.request.environ["beaker.session"]
        if "cpt" in session:
            session["cpt"]+=1
        else:
            session["cpt"]=1
        return "cpt:"+str(session["cpt"])
    
    if __name__=="__main__":
    
        session_opts = {
            "session.type": "file",
            'session.cookie_expires': True,
            'session.auto': True,
            'session.data_dir': "cache",
        }
    
        app = web.default_app()
        
        app = SessionMiddleware(app, session_opts)
    
        web.run(app=app,reloader=True)
    Comments

原文地址:https://www.cnblogs.com/lexus/p/3454761.html