Python full Stack Series---define a session class

Source: Internet
Author: User
Tags md5 encryption

First: Note that the Get_cookie in the cookie is the return string, and Get_secure_cookie returns the byte type

            #self. Get_secure_cookie ()            #The decoded cookie value is returned as a byte string (unlike            # ' Get_cookie ').

MD5 encryption gets the hexadecimal is also the returned string type

        Import hashlib        import time        obj = hashlib.md5 ()        obj.update (bytes (str (time.time ()), encoding= "UTF8")) # Incoming byte type        random_str = obj.hexdigest () #返回字符串        "" "Return the Digest value as a string of hexadecimal digits. """

The following is a custom session class, and using:

Import tornado.web# on the memory redis file database container={} #定义一个session类class session:def __init__ (self,handler): self.h Andler=handler Pass def __genarate_random_str (self): import hashlib import Time obj = HASHL        IB.MD5 () obj.update (bytes (str (time.time ()), encoding= "UTF8")) #传入byte类型 random_str = Obj.hexdigest () #返回字符串        Return random_str def set_value (self,key,value): #在container中加入随机字符串 #加入自定义数据 #在客户端中写入随机字符串 Random_str= ' If Self.handler.get_cookie (' Py_session '): Random_str=self.handler.get_cookie (' py_ses Sion ') if not Container.get (random_str,none): Random_str = Self.__genarate_random_str () E LSE:RANDOM_STR=SELF.__GENARATE_RANDOM_STR () If not Container.get (random_str,none): Container [random_str]={} container[random_str][key]=value #加入客户端 Self.handler.set_cookie (' py_session ', random_s TR) def get_value (selF,key): #先去获取客户端的随机字符串 #从container中获取自定义数据 random_str=self.handler.get_cookie (' py_session ', None) Dict_info=container.get (Random_str,none) if not Dict_info:return None return DICT_INFO[KEY]CL            Indexhandler (Tornado.web.RequestHandler): Def get (self): if self.get_argument (' U ', None) in [' ASD ', ' ZXC ']: s = Session (self) s.set_value (' Is_login ', True) #self. Get_secure_cookie () #The de        Coded cookie value is returned as a byte string (unlike # ' Get_cookie ').        Else:self.write ("Please login") class Managerhandler (Tornado.web.RequestHandler): Def get (self, *args, **kwargs):            S=session (self) val = s.get_value (' is_login ') if Val:self.write ("login Successful") Else: Self.redirect ("/index") settings ={' Template_path ': ' Views ', ' static_path ': ' statics ', ' cookie_secret ': ' Dafawaf Awfaw ',}application = Tornado.web.Application ([(R "/INdex ", Indexhandler), (R"/manager ", Managerhandler),],**settings) 

Python full Stack Series---define a session class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.