s4 index.py
1 import tornado.ioloop 2 import tornado.web 3 import time 4 5 6 class IndexHandler(tornado.web.RequestHandler): 7 def get(self,*args,**kwargs): 8 self.render('index.html') 9 10 class LogoutHandle(tornado.web.RequestHandler): 11 def get(self,*args,**kwargs): 12 self.set_cookie('auth','0') 13 self.redirect('/login') 14 15 class ManagerHandler(tornado.web.RequestHandler): 16 def get(self,*args,**kwargs): 17 co=self.get_cookie('auth') 18 if co == '1': 19 self.render('manager.html') 20 else: 21 self.redirect('/login') 22 23 class LoginHandler(tornado.web.RequestHandler): 24 def get(self,*args,**kwargs): 25 self.render('login.html',Logon_failure='') 26 def post(self, *args, **kwargs): 27 username=self.get_argument('username',None) 28 pwd = self.get_argument('password', None) 29 chexk=self.get_argument('auto',None) 30 if username=='alex' and pwd=='sb': 31 if chexk==1: 32 self.set_cookie('auth', '1', expires_days=7) 33 r=time.time()+100 34 self.set_cookie('auth','1',expires=r) 35 self.redirect('/manager') 36 else: 37 self.render('login.html',Logon_failure="登陆失败") 38 settings={ 39 'template_path':'tpl', 40 'static_path': 'static', 41 } 42 application = tornado.web.Application([ 43 (r"/index", IndexHandler), 44 (r"/manager", ManagerHandler), 45 (r"/login", LoginHandler), 46 (r"/logout", LogoutHandle), 47 ],**settings) 48 49 if __name__ == "__main__": 50 application.listen(8888) 51 tornado.ioloop.IOLoop.instance().start()
manager.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href="/logout">退出</a> <h1>银行卡余额:-1000</h1> </body> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/login" method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit" value="登录"> <input type="checkbox" name="auto" value="1">七天面登录 <span>{{Logon_failure}}</span> </form> </body> </html>
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>首页</h1> </body> </html>