web.py在SAE中的Session問題解決方法(使用mysql存儲)
這段時間一直想嘗試著在SAE中使用Python,初步選擇了Web.py框架做為開發(fā)框架,但是可憐SAE上的資料少的可憐,有點(diǎn)問題基本上解決不了,今天解決一個Session在Session的存儲問題,在SAE中不能直接用本地文件存儲,好像是權(quán)限的原因,我現(xiàn)在采用的是保存在mysql中,效果也不錯。希望對大家有幫助。直接上代碼了。
index.wsgi
#!/usr/bin/env python # coding: utf-8 import os import web import sae from config.url import urls from config import settings #是否具有調(diào)試功能 web.config.debug = False # app = web.application(urls, globals()).wsgifunc() # application = sae.create_wsgi_app(app) #解決Session在SAE中的問題 app = web.application(urls, globals()) #將session保存在數(shù)據(jù)庫中 db = settings.db store = 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-8 import web import sae.const #數(shù)據(jù)庫設(shè)定 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) #模板設(shè)定 render = web.template.render('templates/', cache=False) config = web.storage( email='oooo@qq.com<script cf-hash="f9e31" type="text/javascript"> /* <![CDATA[ */!function(){try{var t="currentScript"in document?document.currentScript:function(){for(var t=document.getElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))return t[e]}();if(t&&t.previousSibling){var e,r,n,i,c=t.previousSibling,a=c.getAttribute("data-cfemail");if(a){for(e="",r=parseInt(a.substr(0,2),16),n=2;a.length-n;n+=2)i=parseInt(a.substr(n,2),16)^r,e+=String.fromCharCode(i);e=document.createTextNode(e),c.parentNode.replaceChild(e,c)}}}catch(u){}}();/* ]]> */</script>', site_name = '任務(wù)跟蹤', site_desc = '', static = '/static', ) web.template.Template.globals['config'] = config web.template.Template.globals['render'] = render login.py #!/usr/bin/env python # coding: utf-8 import web from config import settings render = settings.render def myloadhook(): global session session = web.config._session class LoginUser: def GET(self): return render.LoginUser() class CheckUser: def POST(self): #獲取Session相關(guān)信息 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')
相關(guān)文章
理解Django 中Call Stack機(jī)制的小Demo
這篇文章主要介紹了理解Django 中Call Stack 機(jī)制的小Demo,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python使用參數(shù)對嵌套字典進(jìn)行取值的方法
這篇文章主要介紹了python使用參數(shù)對嵌套字典進(jìn)行取值,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04Python開發(fā)技巧之海象運(yùn)算符的三種運(yùn)用方式
這篇文章主要介紹了Python開發(fā)技巧之海象運(yùn)算符的三種運(yùn)用方式,海象運(yùn)算符是在 PEP 572 被提出的,直到 3.8 版本合入發(fā)布,感興趣的同學(xué)可以參考學(xué)習(xí)下2021-10-10Python監(jiān)控服務(wù)器實(shí)用工具psutil使用解析
這篇文章主要介紹了Python監(jiān)控服務(wù)器實(shí)用工具psutil使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12selenium WebDriverWait類等待機(jī)制的實(shí)現(xiàn)
這篇文章主要介紹了selenium WebDriverWait類等待機(jī)制的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03在Python下進(jìn)行UDP網(wǎng)絡(luò)編程的教程
這篇文章主要介紹了在Python下進(jìn)行UDP網(wǎng)絡(luò)編程的教程,UDP編程是Python網(wǎng)絡(luò)編程部分的基礎(chǔ)知識,示例代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04django 解決manage.py migrate無效的問題
今天小編就為大家分享一篇django 解決manage.py migrate無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05