乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      python3+flask+mysql實現(xiàn)登錄注冊

       刮骨劍 2019-04-08

      簡述

      最近開始學習python,就做了一個簡單的web服務(wù)。這個服務(wù)中用了Flask框架,這個能夠快速地搭建web服務(wù)器,詳細過程請看鏈接描述。創(chuàng)建的文件目錄如下

      clipboard.png

      前臺頁面

      在前臺頁面中只需創(chuàng)建基本的表單內(nèi)容,method使用get,登錄和注冊的action分別為/login、/registuser,代碼如下所示:

      <!--登錄模塊--!>
      <!DOCTYPE html>
      <html lang="en">
          <head>
              <title></title>
              <meta charset="UTF-8">
              <meta name="viewport" content="width=device-width, initial-scale=1">
              <link href="css/style.css" rel="stylesheet">
          </head>
          <body>
              <form method="get" action='/login'>
                  <label>用戶名:<input type="text" name="user" value=""></label><br>
                  <label>密碼:<input type="password" name="password" value=""></label><br>
                  <input type="submit" value="登錄">
              </form>
          </body>
      </html>
      <!--注冊模塊--!>
      <!DOCTYPE html>
      <html lang="en">
          <head>
              <title></title>
              <meta charset="UTF-8">
              <meta name="viewport" content="width=device-width, initial-scale=1">
              <link href="css/style.css" rel="stylesheet">
          </head>
          <body>
              <form method="get" action='/registuser'>
                  <label>用戶名:<input type="text" name="user" value=""></label>
                  <label>密碼:<input type="password" name="password" value=""></label>
                  <input type="submit" value="注冊">
              </form>
          </body>
      </html>

      導入模塊

      python使用Flask需要導入Flask模塊,如果沒有這個模塊就參見前面的鏈接去安裝。使用mysql需要導入pymysql模塊,這個具體是什么,請參見鏈接描述??傊?,python需要用到特定的模塊的時候,需要去導入模塊,本例具體導入的內(nèi)容如下所示:

      #導入數(shù)據(jù)庫模塊
      import pymysql
      #導入Flask框架,這個框架可以快捷地實現(xiàn)了一個WSGI應(yīng)用 
      from flask import Flask
      #默認情況下,flask在程序文件夾中的templates子文件夾中尋找模塊
      from flask import render_template
      #導入前臺請求的request模塊
      from flask import request   
      import traceback  
      #傳遞根目錄
      app = Flask(__name__)

      路由訪問靜態(tài)文件

      在Flask創(chuàng)建服務(wù)器后,照著前面圖片的文件目錄,通過下面的路由就能通過服務(wù)器訪問靜態(tài)的html文件,代碼如下所示:

      #默認路徑訪問登錄頁面
      @app.route('/')
      def login():
          return render_template('login.html')
      
      #默認路徑訪問注冊頁面
      @app.route('/regist')
      def regist():
          return render_template('regist.html')

      此時在瀏覽器中通過localhost:5000(Flask默認端口為5000)就能訪問login.html文件,localhost:5000/regist就能訪問regist.html。

      處理前臺請求及對數(shù)據(jù)庫進行操作

      跟所有的web服務(wù)思路一樣,獲取前臺的請求,根據(jù)不同的請求路由執(zhí)行不同的函數(shù)內(nèi)容。這里以注冊為例,當表單填寫完后,點擊注冊按鈕,數(shù)據(jù)向后臺請求,請求路由為/registuser。python根據(jù)相對應(yīng)的路由執(zhí)行函數(shù),這里函數(shù)處理的內(nèi)容是把前臺的用戶名和密碼插入到數(shù)據(jù)庫中(前提是已經(jīng)在數(shù)據(jù)庫中創(chuàng)建好了user表)。插入成功就跳轉(zhuǎn)到登錄頁面,插入失敗就返回注冊失敗的內(nèi)容。登錄的邏輯內(nèi)容也類似,就不再細述,具體代碼如下:

      #獲取注冊請求及處理
      @app.route('/registuser')
      def getRigistRequest():
      #把用戶名和密碼注冊到數(shù)據(jù)庫中
      
          #連接數(shù)據(jù)庫,此前在數(shù)據(jù)庫中創(chuàng)建數(shù)據(jù)庫TESTDB
          db = pymysql.connect("localhost","root","123456","TESTDB" )
          # 使用cursor()方法獲取操作游標 
          cursor = db.cursor()
          # SQL 插入語句
          sql = "INSERT INTO user(user, password) VALUES ("+request.args.get('user')+", "+request.args.get('password')+")"
          try:
              # 執(zhí)行sql語句
              cursor.execute(sql)
              # 提交到數(shù)據(jù)庫執(zhí)行
              db.commit()
               #注冊成功之后跳轉(zhuǎn)到登錄頁面
              return render_template('login.html') 
          except:
              #拋出錯誤信息
              traceback.print_exc()
              # 如果發(fā)生錯誤則回滾
              db.rollback()
              return '注冊失敗'
          # 關(guān)閉數(shù)據(jù)庫連接
          db.close()
      #獲取登錄參數(shù)及處理
      @app.route('/login')
      def getLoginRequest():
      #查詢用戶名及密碼是否匹配及存在
          #連接數(shù)據(jù)庫,此前在數(shù)據(jù)庫中創(chuàng)建數(shù)據(jù)庫TESTDB
          db = pymysql.connect("localhost","root","123456","TESTDB" )
          # 使用cursor()方法獲取操作游標 
          cursor = db.cursor()
          # SQL 查詢語句
          sql = "select * from user where user="+request.args.get('user')+" and password="+request.args.get('password')+""
          try:
              # 執(zhí)行sql語句
              cursor.execute(sql)
              results = cursor.fetchall()
              print(len(results))
              if len(results)==1:
                  return '登錄成功'
              else:
                  return '用戶名或密碼不正確'
              # 提交到數(shù)據(jù)庫執(zhí)行
              db.commit()
          except:
              # 如果發(fā)生錯誤則回滾
              traceback.print_exc()
              db.rollback()
          # 關(guān)閉數(shù)據(jù)庫連接
          db.close()
          
      
      #使用__name__ == '__main__'是 Python 的慣用法,確保直接執(zhí)行此腳本時才
      #啟動服務(wù)器,若其他程序調(diào)用該腳本可能父級程序會啟動不同的服務(wù)器
      if __name__ == '__main__':
          app.run(debug=True)

      此例僅僅是為完成python作為web服務(wù)器的流程,很多檢驗控制就沒細細去做。

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多