zoukankan      html  css  js  c++  java
  • python_way ,自定义session

    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 =="123":                                 #从数据库中查找出来的用户名密码
                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()

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    电磁学讲义3:电场
    电磁学讲义2:库仑定律
    电磁学讲义1:静电的基本现象
    安卓(Android)手机如何安装APK?
    理论物理极础9:相空间流体和吉布斯-刘维尔定理
    物理学家的LOGO
    Zhulina 的高分子刷理论
    一步一步学Silverlight 2系列(5):实现简单的拖放功能
    地图上显示X,Y 坐标代码
    一步一步学Silverlight 2系列(4):鼠标事件处理
  • 原文地址:https://www.cnblogs.com/python-way/p/6045096.html
Copyright © 2011-2022 走看看