程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 更多關於編程 >> web.py在SAE中的Session問題解決方法

web.py在SAE中的Session問題解決方法

編輯:更多關於編程

       這篇文章主要介紹了web.py在SAE中的Session問題解決方法(使用mysql存儲),本文直接給出實現代碼,代碼中包含詳細注釋,需要的朋友可以參考下

      這段時間一直想嘗試著在SAE中使用Python,初步選擇了Web.py框架做為開發框架,但是可憐SAE上的資料少的可憐,有點問題基本上解決不了,今天解決一個Session在Session的存儲問題,在SAE中不能直接用本地文件存儲,好像是權限的原因,我現在采用的是保存在mysql中,效果也不錯。希望對大家有幫助。直接上代碼了。

      index.wsgi

      ?

    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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 #!/usr/bin/env python # coding: utf-8 import os import web import sae from config.url import urls from 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.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 #數據庫設定 db = web.database(dbn='mysql', user=sae.const.MYSQL_USER, pw=sae.const.MYSQL_PASS, host=sae.const.MYS QL _HOST, port=3307, db=sae.const.MYSQL_DB) #模板設定 render = web.template.render('templates/', cache=False)   config = web.storage( email='[email protected]<script cf-hash="f9e31" type="text/javascript"> /* <![CDATA[ */!function(){try{var t="currentScript"in document?document.curren tScript:function(){for(var t=document.g etElementsByTagName("script"),e=t.length;e--;)if(t[e].getAttribute("cf-hash"))retu rn 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.fro mCharCode(i);e=document.createTextNode(e),c.parentNode.repl aceChild(e,c)}}}catch(u){}}();/* ]]> */</script>', site_name = '任務跟蹤', 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相關信息 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')
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved