python使用tornado實(shí)現(xiàn)登錄和登出
本文實(shí)例為大家分享了tornado實(shí)現(xiàn)登錄和登出的具體代碼,供大家參考,具體內(nèi)容如下
main.py如下:
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.options
import os.path
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie("username")
class LoginHandler(BaseHandler):
def get(self):
self.render('login.html')
def post(self):
self.set_secure_cookie("username", self.get_argument("username"))
self.redirect("/")
class WelcomeHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render('index.html', user=self.current_user)
class LogoutHandler(BaseHandler):
def post(self):
if (self.get_argument("logout", None)):
self.clear_cookie("username")
self.redirect("/")
if __name__ == "__main__":
tornado.options.parse_command_line()
settings = {
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
"cookie_secret": "bZJc2sWbQLKos6GkHn/VB9oXwQt8S0R0kRvJ5/xJ89E=",
"login_url": "/login"
}
application = tornado.web.Application([
(r'/', WelcomeHandler),
(r'/login', LoginHandler),
(r'/logout', LogoutHandler)
],debug= True,**settings)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
index.html
<html>
<head>
</head>
<body>
<p>Hello {{ user }}</p>
<form action="/logout?logout=1" method="post">
<input type="submit" value="Log out"></br>
</body>
</html>
login.html
<html> <head> </head> <body> <h>Login Page</h> <form action="/login" method="post">Name:<input type="text" name="username"></br> <input type="submit" value="Sign in"></br> </form> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python?gravis庫實(shí)現(xiàn)圖形數(shù)據(jù)可視化實(shí)例探索
這篇文章主要為大家介紹了python?gravis庫實(shí)現(xiàn)圖形數(shù)據(jù)可視化實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02
django日志默認(rèn)打印request請求信息的方法示例
這篇文章主要給大家介紹了關(guān)于django日志默認(rèn)打印request請求信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
基于python OpenCV實(shí)現(xiàn)動(dòng)態(tài)人臉檢測
這篇文章主要為大家詳細(xì)介紹了基于python OpenCV實(shí)現(xiàn)動(dòng)態(tài)人臉檢測,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
opencv中cv2.minAreaRect函數(shù)輸出角度問題詳解
minAreaRect返回的數(shù)據(jù)包括了矩形的中心點(diǎn),寬、高,和旋轉(zhuǎn)角度,下面這篇文章主要給大家介紹了關(guān)于opencv中cv2.minAreaRect函數(shù)輸出角度問題的相關(guān)資料,需要的朋友可以參考下2022-11-11
Python實(shí)現(xiàn)對Excel文件中不在指定區(qū)間內(nèi)的數(shù)據(jù)加以去除的方法
這篇文章主要介紹了基于Python語言,讀取Excel表格文件,基于我們給定的規(guī)則,對其中的數(shù)據(jù)加以篩選,將不在指定數(shù)據(jù)范圍內(nèi)的數(shù)據(jù)剔除,保留符合我們需要的數(shù)據(jù)的方法,需要的朋友可以參考下2023-08-08

