一、準備登陸的htmlhtml
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="icon" href="favicon.ico"> </head> <body> <!--提交的位置 點擊提交按鈕至關於請求了這個網址--> <form action="http://127.0.0.1:9000/auth" method="get"> <input type="text" name="username" placeholder="username"> <input type="password" name="password" placeholder="password"> <button>提交</button> </form> </body> </html> 登陸頁面
二、將HTML頁面發送到服務端mysql
# -*- coding: utf-8 -*- # @Time : 2019/5/16 19:16 import webauth from wsgiref.simple_server import make_server from urllib.parse import parse_qs def auth(environ): # 判斷提交按鈕的請求是什麼請求 if environ.get("REQUEST_METHOD")=="GET": # 獲取用戶輸入的信息 username=xiaohei&password=123 request_data = environ['QUERY_STRING'] # 對信息進行解析 {'username': ['xiaohei'], 'password': ['123']} re_data = parse_qs(request_data) # 分別取出用戶名和密碼 username = re_data['username'][0] password = re_data['password'][0] # 提交給服務器進行驗證 ret = webauth.auth(username,password) # 服務器返回驗證信息 if ret: with open("websuccess.html", "rb") as f: content = f.read() return [content] else: # 驗證失敗 return [b"login failure"] def login(environ): with open("login.html","rb") as f: content = f.read() return [content] def log(environ): with open("favicon.ico","rb") as f: content = f.read() return [content] li = [ ("/login",login), ("/favicon.ico",log), ("/auth",auth), ] def app(environ, start_response): # 封裝響應信息 start_response('200 OK', [('Content-Type', 'text/html'), ('k1', 'v1')]) # environ 封裝好的請求數據,字典的格式 path = environ["PATH_INFO"] for i in li: # 判斷用戶輸入的url if i[0] == path: # 調用url對應的函數 ret = i[1](environ) return ret break else: # 用戶輸入的網址不合法 return [b"404 not found"] # 綁定ip和端口,有人鏈接就調用app函數 httpp = make_server("127.0.0.1",9000,app) # 開始監聽http請求 httpp.serve_forever() 服務端頁面
三、數據庫驗證登陸信息web
# -*- coding: utf-8 -*- # @Time : 2019/5/16 19:42 import pymysql # 數據庫信息驗證,驗證用戶名和密碼 def auth(username,password): db = pymysql.connect( host="127.0.0.1", port=3306, user="root", password="root", database="day53", charset="utf8" # 不能寫- ) cursor = db.cursor(pymysql.cursors.DictCursor) sql = "select * from info where username=%s and password=%s;" res = cursor.execute(sql,[username,password]) print(res) if res: return True else: return False 驗證登陸
四、返回登陸成功頁面sql