web.py在SAE中的Session問題解決方案(使用mysql儲存)

來源:互聯網
上載者:User
這段時間一直想嘗試著在SAE中使用Python,初步選擇了Web.py架構做為開發架構,但是可憐SAE上的資料少的可憐,有點問題基本上解決不了,今天解決一個Session在Session的儲存問題,在SAE中不能直接用本地檔案儲存體,好像是許可權的原因,我現在採用的是儲存在mysql中,效果也不錯。希望對大家有協助。直接上代碼了。

index.wsgi

#!/usr/bin/env python# coding: utf-8import osimport webimport saefrom config.url import urlsfrom config import settings #是否具有調試功能web.config.debug = False# app = web.application(urls, globals()).wsgifunc()# application = sae.create_wsgi_app(app) #解決Session在SAE中的問題app = web.application(urls, globals()) #將session儲存在資料庫中db = settings.dbstore = web.session.DBStore(db, 'sessions')#session = web.session.Session(app, store, initializer={'access_token': 'true'})session = web.session.Session(app, store)web.config._session = session application = sae.create_wsgi_app(app.wsgifunc())url.py#!/usr/bin/env python# coding: utf-8 pre_fix = 'controllers.' urls = (  '/',          pre_fix + 'todo.Index',  '/todo/new',      pre_fix + 'todo.New',  '/todo/(\d+)',     pre_fix + 'todo.View',  '/todo/(\d+)/edit',   pre_fix + 'todo.Edit',  '/todo/(\d+)/delete',  pre_fix + 'todo.Delete',  '/todo/(\d+)/finish',  pre_fix + 'todo.Finish',  '/todo/login', pre_fix + 'login.LoginUser',  '/todo/checkuser',pre_fix+'login.CheckUser',  '/todo/reset',pre_fix+'todo.reset',  '/todo/saveupload','mycontrollers.saveupload.SaveUpload')setting.py#!/usr/bin/env python# coding: utf-8import webimport sae.const#資料庫設定db = web.database(dbn='mysql', user=sae.const.MYSQL_USER, pw=sae.const.MYSQL_PASS, host=sae.const.MYSQL_HOST, port=3307, db=sae.const.MYSQL_DB)#模板設定render = web.template.render('templates/', cache=False) config = web.storage(  email='oooo@qq.com',  site_name = '任務跟蹤',  site_desc = '',  static = '/static',) web.template.Template.globals['config'] = configweb.template.Template.globals['render'] = renderlogin.py#!/usr/bin/env python# coding: utf-8import webfrom config import settingsrender = settings.renderdef myloadhook():  global session  session = web.config._sessionclass LoginUser:  def GET(self):    return render.LoginUser()class CheckUser:  def POST(self):    #擷取Session相關資訊    myloadhook()    #擷取表單資訊    i = web.input()    username =i.get('txtUserName',None)    password=i.get('txtUserPass',None)    #從全域設定檔中得到session    session = web.config._session    if username == 'chu888' and password == 'chu888':      session.access_token = 'true'      raise web.seeother('/')    else:      session.access_token = 'false'      raise web.seeother('/todo/login')
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.