zoukankan      html  css  js  c++  java
  • Web.py session用户认证

    调试一个用session来认证用户的程序 大概是这个样子 基本上可以当作webpy session认证的示例程序了

    #!/usr/bin/env python
    #coding=utf-8
    import web
    import time
    
    urls = (
        '/', 'index',
        '/xml', 'pushxml',
        '/login', 'login',
        '/logout', 'logout',
    )
    render = web.template.render('templates/')
    web.config.debug = False
    app = web.application(urls, locals())
    session = web.session.Session(app, web.session.DiskStore('sessions'))      
    
    class index():
        def GET(self):
            try:
                if session.logged_in == True:
                    return '<h1>You are logged in</h1><a href="/logout">Logout</a>'
            except AttributeError:
                pass
            return '<h1>You are not logged in.</h1><a href="/login">Login now</a>'
    
    def authorize(func):
        def logged(*args,**dic):
            if session.logged_in==True:
                func(*args,**dic)
            else:
                raise web.seeother('/login')
        return logged
    
    class pushxml():
        # @authorize
        def GET(self):
            try:
                if session.logged_in == True:
                    web.header('Content-Type', 'text/xml')
                    i = web.input(data=None)
                    return render.response(i.data)
            except AttributeError:
                pass
           
    class login():
        def GET(self):
            try:
                session.logged_in = False
            except AttributeError:
                pass
            return """
                <form action=/login method=POST>
                    <table id="login">
                        <tr>
                            <td>User: </td>
                            <td><input type=text name='user'></td>
                        </tr>
                        <tr>
                            <td>Password: </td>
                            <td><input type="password" name=passwd></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type=submit value=LOGIN></td>
                        </tr>
                    </table>
                </form>
            """
               
        def POST(self):
            login_data = web.input()
            if login_data.user == 'a' and login_data.passwd == 'a':
                session.logged_in = True
                print "posted"
                print session
                raise web.seeother('/')
           
    class logout():
        def GET(self):
            try:
                session.logged_in = False
                session.kill()
            except AttributeError:
                pass
            raise web.seeother('/')
    
    if __name__ == '__main__':
        app.run()
    
    

      开始的时候为了方便web.config.debug = True(这是默认值) session总是不能保存 经过相当长时间的纠结发现错误竟然出在这里

    web.config.debug = False

    如上 关掉debug模式session可以正常保存了 究竟失身么原因我也搞不清楚了 今天才看到 http://webpy.org/cookbook/sessions

    sessions doesn’t work in debug mode because it interfere with reloading. see session_with_reloader for more details.

    FUCK web.py!!!

     
  • 相关阅读:
    前端分页功能的实现以及原理
    Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
    Jquery实现的几款漂亮的时间轴
    jQuery点击弹出层,弹出模态框,点击模态框消失
    如何用CSS快速布局(一)—— 布局元素详细
    验证控件jQuery Validation Engine调用外部函数验证
    Javascript实现页面跳转的几种方式
    最详细win7下手动搭建PHP环境:apache2.4.23+php7.0.11
    spark调优——JVM调优
    spark调优——Shuffle调优
  • 原文地址:https://www.cnblogs.com/51reboot/p/4006029.html
Copyright © 2011-2022 走看看