cookie模拟登陆:

import tornado.web

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        #self.write("Hello world")
        # 展示所有的cookie
        # print(self.cookies)
        # print(self.get_cookie('k1'))
        # self.set_cookie('k1','999')#还有 过期时间 适用路径
        # self.render("index.html")
        if self.get_argument('u',None) in ['asd','zxc']:
            self.set_secure_cookie('user',self.get_argument('u'))
        else:
            self.write('请登录')

class ManagerHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        #默认self.get_secure_cookie('user',None)获取的数据是bytes类型
        if str(self.get_secure_cookie('user',None),encoding="utf8") in ['asd','zxc']:
            self.write("欢迎登录:"+str(self.get_secure_cookie('user'),encoding="utf8"))
        else:
            self.redirect('/index')

settings ={
    'template_path':'views',
    'static_path':'statics',
    'cookie_secret':'dafawafawfaw',
}

application = tornado.web.Application([
    (r"/index",IndexHandler),
    (r"/manager",ManagerHandler),
],**settings)


if __name__=="__main__":
    application.listen(8080)
    tornado.ioloop.IOLoop.instance().start()
View Code

相关文章:

  • 2021-12-29
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2021-12-20
  • 2021-11-09
猜你喜欢
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2021-12-03
相关资源
相似解决方案