免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

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

簡述

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

前臺頁面

在前臺頁面中只需創(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模塊,這個具體是什么,請參見鏈接描述??傊琾ython需要用到特定的模塊的時候,需要去導入模塊,本例具體導入的內(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ù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
python+ajax+mysql簡單前后端數(shù)據(jù)庫的搭建
python模塊之MySQLdb: 用python連接mysql數(shù)據(jù)庫
零基礎(chǔ)如何自學Python
Python中SQLite數(shù)據(jù)庫使用初步
Python操作mysql數(shù)據(jù)庫:MySQLdb模塊
Python訪問數(shù)據(jù)庫
更多類似文章 >>
生活服務(wù)
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服