web.py在SAE中的Session问题解决方法(使用mysql存储)

yipeiwu_com5年前Python基础

这段时间一直想尝试着在SAE中使用Python,初步选择了Web.py框架做为开发框架,但是可怜SAE上的资料少的可怜,有点问题基本上解决不了,今天解决一个Session在Session的存储问题,在SAE中不能直接用本地文件存储,好像是权限的原因,我现在采用的是保存在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
 
#是否具有调试功能
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.MYSQL_HOST, port=3307, db=sae.const.MYSQL_DB)
#模板设定
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 = '任务跟踪',
  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')

相关文章

Python的垃圾回收机制深入分析

一、概述: Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾。在引用计数的基础上,还可以通过“标记-清除”(mark and swee...

Python学生信息管理系统修改版

在学习之前先要了解sqlite游标的使用方法python使用sqlite3时游标的使用方法 继上篇博客Python实现学生信息管理系统后,我就觉得写的太复杂了,然后又是一通优化、优化...

在Python的Flask框架中验证注册用户的Email的方法

在Python的Flask框架中验证注册用户的Email的方法

本教程详细介绍在用户注册过程中如何去验证他们的email地址。 工作流程上来讲,在用户注册一个新账户后会寄送一个确认信。直到用户按指示完成了邮件中的“验证”,否则他们的账户会一直处于“未...

rabbitmq(中间消息代理)在python中的使用详解

rabbitmq(中间消息代理)在python中的使用详解

在之前的有关线程,进程的博客中,我们介绍了它们各自在同一个程序中的通信方法。但是不同程序,甚至不同编程语言所写的应用软件之间的通信,以前所介绍的线程、进程队列便不再适用了;此种情况便只能...

Pytorch之contiguous的用法

contiguous tensor变量调用contiguous()函数会使tensor变量在内存中的存储变得连续。 contiguous():view只能用在contiguous的var...