溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python使用tornado實現登錄和登出

發布時間:2020-09-18 11:57:31 來源:腳本之家 閱讀:373 作者:bayron 欄目:開發技術

本文實例為大家分享了tornado實現登錄和登出的具體代碼,供大家參考,具體內容如下

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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女