Web.py session使用者認證

來源:互聯網
上載者:User

標籤:http   color   io   os   ar   for   sp   div   c   

調試一個用session來認證使用者的程式 大概是這個樣子 基本上可以當作webpy session認證的樣本程式了

#!/usr/bin/env python#coding=utf-8import webimport timeurls = (    ‘/‘, ‘index‘,    ‘/xml‘, ‘pushxml‘,    ‘/login‘, ‘login‘,    ‘/logout‘, ‘logout‘,)render = web.template.render(‘templates/‘)web.config.debug = Falseapp = 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 loggedclass 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!!!

 

Web.py session使用者認證

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.