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

作者:junjie 时间:2024-01-28 02:17:15 

这段时间一直想尝试着在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')
标签:web.py,SAE,Session,mysql,存储
0
投稿

猜你喜欢

  • python二叉树类以及其4种遍历方法实例

    2023-07-25 02:22:48
  • python对验证码降噪的实现示例代码

    2021-01-02 09:54:17
  • vue 页面卡死,点击无反应的问题及解决

    2024-04-27 16:04:21
  • python音频处理用到的操作的示例代码

    2021-05-11 22:05:19
  • 从两个方面讲解SQL Server口令的脆弱性

    2009-01-08 13:40:00
  • django中操作mysql数据库的方法

    2024-01-18 13:37:55
  • Python端口扫描简单程序

    2022-12-26 02:19:45
  • Python plt 利用subplot 实现在一张画布同时画多张图

    2021-02-04 02:50:17
  • python pandas模糊匹配 读取Excel后 获取指定指标的操作

    2022-07-24 07:48:32
  • asp如何自动更新导航栏?

    2010-07-07 12:10:00
  • Vue中的 DOM与Diff详情

    2023-07-02 16:32:37
  • vue-router权限控制(简单方式)

    2024-05-28 15:42:35
  • Python代码打开本地.mp4格式文件的方法

    2021-08-27 21:43:04
  • php动态函数调用方法

    2023-11-15 00:18:30
  • C#使用ADO.Net连接数据库与DbProviderFactory实现多数据库访问

    2024-01-24 08:56:29
  • 服务端XMLHTTP(ServerXMLHTTP in ASP)基本应用(上)

    2008-11-11 12:49:00
  • 基于python实现模拟数据结构模型

    2022-11-12 23:44:01
  • Python参数解析器configparser简介

    2021-04-22 02:23:31
  • python 自定义异常和主动抛出异常(raise)的操作

    2022-03-22 12:43:22
  • sql server 常用的几个数据类型

    2024-01-14 16:39:34
  • asp之家 网络编程 m.aspxhome.com