簡述最近開始學(xué)習(xí)python,就做了一個簡單的web服務(wù)。這個服務(wù)中用了Flask框架,這個能夠快速地搭建web服務(wù)器,詳細(xì)過程請看鏈接描述。創(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>
導(dǎo)入模塊python使用Flask需要導(dǎo)入Flask模塊,如果沒有這個模塊就參見前面的鏈接去安裝。使用mysql需要導(dǎo)入pymysql模塊,這個具體是什么,請參見鏈接描述。總之,python需要用到特定的模塊的時候,需要去導(dǎo)入模塊,本例具體導(dǎo)入的內(nèi)容如下所示: #導(dǎo)入數(shù)據(jù)庫模塊
import pymysql
#導(dǎo)入Flask框架,這個框架可以快捷地實現(xiàn)了一個WSGI應(yīng)用
from flask import Flask
#默認(rèn)情況下,flask在程序文件夾中的templates子文件夾中尋找模塊
from flask import render_template
#導(dǎo)入前臺請求的request模塊
from flask import request
import traceback
#傳遞根目錄
app = Flask(__name__)
路由訪問靜態(tài)文件在Flask創(chuàng)建服務(wù)器后,照著前面圖片的文件目錄,通過下面的路由就能通過服務(wù)器訪問靜態(tài)的html文件,代碼如下所示: #默認(rèn)路徑訪問登錄頁面
@app.route('/')
def login():
return render_template('login.html')
#默認(rèn)路徑訪問注冊頁面
@app.route('/regist')
def regist():
return render_template('regist.html')
此時在瀏覽器中通過localhost:5000(Flask默認(rèn)端口為5000)就能訪問login.html文件,localhost:5000/regist就能訪問regist.html。 處理前臺請求及對數(shù)據(jù)庫進行操作跟所有的web服務(wù)思路一樣,獲取前臺的請求,根據(jù)不同的請求路由執(zhí)行不同的函數(shù)內(nèi)容。這里以注冊為例,當(dāng)表單填寫完后,點擊注冊按鈕,數(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)容也類似,就不再細(xì)述,具體代碼如下: #獲取注冊請求及處理
@app.route('/registuser')
def getRigistRequest():
#把用戶名和密碼注冊到數(shù)據(jù)庫中
#連接數(shù)據(jù)庫,此前在數(shù)據(jù)庫中創(chuàng)建數(shù)據(jù)庫TESTDB
db = pymysql.connect("localhost","root","123456","TESTDB" )
# 使用cursor()方法獲取操作游標(biāo)
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()方法獲取操作游標(biāo)
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ù)器的流程,很多檢驗控制就沒細(xì)細(xì)去做。
|