python_way ,自定义session


container = {}     #可以是数据库,可以是缓存也可以是文件

class Session:
def __init__(self, handler): #handler就是之前传递过来的handler方法,所以它也会有setcookie方法。
#self.r_str 当前用户的cookie
self.handler = handler
self.r_str = handler.get_cookie("__session_id__") #获取客户端的cookie
if self.r_str: #如果获取到cookie
if self.r_str in container: #检查这个cookie是否存在在container中
#如果客户端访问的md5在我的列表中证明是真的
print("有cookie")
self.r_str = self.r_str
print(self.r_str)#设置一个字段,以备以后调用
else:#否则就是假的
self.cookie = client_publish.md5_str() # 先生成一个变化的cookie
handler.set_cookie("__session_id__",self.cookie) #设置一个新的cookie
print("假的cookie")
container[self.cookie] = {} #把这个cookie放在字典中
self.r_str = self.cookie #同样因为是新生成的cookie,还是设置一个字段,以备后患
else:
print("没有cookie")
self.cookie = client_publish.md5_str()
self.r_str = self.cookie
container[self.r_str] = {} # 如果没有设置cookie,第一次访问
handler.set_cookie("__session_id__", self.r_str, expires=time.time() + 20) # 设置cookie并且设置超时时间,每次用户访问都设置一下 def set_session(self, key, value):
print(container)
container[self.r_str][key] = value #设置session 这样可以保存很多key和value
print(container) def get_session(self):
user_session = self.handler.get_cookie("__session_id__", None)
if user_session in container:
try:
if container[user_session]["is_login"]:
return True
except Exception as e:
print(e,"not value")
return False class MyRequestHandler(tornado.web.RequestHandler):
def initialize(self):
#在RequestHandler中有set_cookie方法
self.key = Session(self) #我们把self传递给Session 自定义的这个类中 class HomeHadler(MyRequestHandler):
def get(self):
#self.set_cookie() 因为这里继承了MyRequestHandler,所以在这里也有set cookie方法
session = Session(self)
user_login = session.get_session()
if user_login:
self.redirect("/index") #跳转到这个用户已经登陆
else:
self.render("login.html") #如果这个用户没有登陆,则进入登陆页面 def post(self):
user = self.get_argument("user")
pwd = self.get_argument("pass")
print(user, pwd)
if user == "hanxu" and pwd =="": #从数据库中查找出来的用户名密码
session = Session(self)
session.set_session("is_login", True)
session.set_session("user", user)
self.redirect("/index") class HostHadler2(MyRequestHandler): #这种方式的话就可以方便统一使用,统一修改了
def get(self):
session = Session(self)
user_session = session.get_session()
if user_session:
self.write("主页")
else:                 #如果没有登陆,就调回登陆页面
self.redirect("/home")
#!/usr/bin/env python3
# Created by han on 2016/10/23
import os, sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from controller import home
import tornado.ioloop
import tornado.web
import publish
import modules #配置模板路径
settings = {
"template_path":"template", #自定义页面放置位置
"static_path": "static", #自定义静态文件放置位置,定义好以后在html页面中写上link标签引入静态文件才能生效
"ui_methods": publish,
"ui_modules": modules,
} application = tornado.web.Application([
(r"/main", home.MainHadler),
], **settings) #只需要在application这里增加setttings这个配置 application.add_handlers("cmdb.old.com",[
(r"/home", home.HomeHadler),
]) application.add_handlers("cmdb.old.com",[
(r"/main", home.CmdbHander),
]) application.add_handlers("cmdb.old.com",[
(r"/index", home.HostHadler2),
]) if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

最新文章

  1. 微信SDK开发——接口接入
  2. [大数据之Sqoop] —— 什么是Sqoop?
  3. SQL Server 在数据库中查找字符串(不知道表名的情况下 查找字符串)
  4. 淘宝UWP桌面版公测-谁需要邀请码?
  5. HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)
  6. ZOJ3741 状压DP Eternal Reality
  7. 模拟 POJ 1068 Parencodings
  8. svn忽略target
  9. Java沙箱技术
  10. 模板类之间的友元关系实现Blob和BlobPtr
  11. [转] unix/linux下线程私有数据实现原理及使用方法
  12. Java正则表达式例子汇总
  13. 0_Simple__cppIntegration
  14. JAVA_SE基础——41.instanceof关键字(运算符)
  15. Hadoop-HA机制工作原理
  16. Json常用序列化工具包大比拼
  17. app测试自动化之定位元素
  18. dp HDU - 5074
  19. oracle11g数据库升级数据库升级
  20. Matlab中imread函数使用报错“不应为MATLAB 表达式”分析

热门文章

  1. TreeNode动态邦定事件
  2. memcached启动参数
  3. BZOJ 1588:营业额统计(Splay)
  4. PHP学习当中遗漏的知识点
  5. oracle-odu小试牛刀--恢复drop表的数据
  6. jquery中用jqzoom实现放大镜效果
  7. gridview 经典
  8. 去掉DataTable中重复的行
  9. php获取json文件数据并动态修改网站头部文件meta信息 --基于CI框架
  10. protobuf安装