1、web框架html
web框架(web framwork)是一種開發框架,用來支持動態網站,網絡應用和網絡服務的開發。這大多數的web框架提供了一套開發和部署網站的方式,也爲web行爲提供了一套通用的方法。web框架已經實現了不少功能,開發人員使用框架提供的方法而且完成本身的業務邏輯,就能快速開發web應用了。瀏覽器和服務器的是基於HTTP協議進行通訊的。也能夠說web框架就是在以上十幾行代碼基礎張擴展出來的,有不少簡單方便使用的方法,大大提升了開發的效率。python
2、wsgir模塊 mysql
最簡單的Web應用就是先把HTML用文件保存好,用一個現成的HTTP服務器軟件,接收用戶請求,從文件中讀取HTML,返回。web
若是要動態生成HTML,就須要把上述步驟本身來實現。不過,接受HTTP請求、解析HTTP請求、發送HTTP響應都是苦力活,若是咱們本身來寫這些底層代碼,還沒開始寫動態HTML呢,就得花個把月去讀HTTP規範。sql
正確的作法是底層代碼由專門的服務器軟件實現,咱們用Python專一於生成HTML文檔。由於咱們不但願接觸到TCP鏈接、HTTP原始請求和響應格式,因此,須要一個統一的接口協議來實現這樣的服務器軟件,讓咱們專心用Python編寫Web業務。這個接口就是WSGI:Web Server Gateway Interface。而wsgiref模塊就是python基於wsgi協議開發的服務模塊。數據庫
wsgiref瀏覽器
from wsgiref.simple_server import make_server def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return [b'<h1>Hello, Web!</h1>'] httpd = make_server('127.0.0.1', 8080, application) print('Servering HTTP on port 8080...') # 開始監聽HTTP請求 httpd.serve_forever()
3、DIY一個web框架服務器
models.py網絡
import pymysql # 鏈接數據庫 conn = pymysql.connect(host='IP', port=3306, user='root', passwd='123', db='blog') #db:庫名 #建立遊標 cur = conn.cursor() # sql=''' # create table userinfo( # id int primary key, # name varchar(32), # password varchar(32) # )''' # cur.execute(sql) # sql = "insert into userinfo values (%s,%s,%s)" # var = [(1, 'mike', 123), (2, 'tom', 123)] # cur.executemany(sql, var) sql = ("select * from userinfo where name='mike'") cur.execute(sql) # 提交 conn.commit() # 關閉指針對象 cur.close() # 關閉鏈接對象 conn.close()
啓動文件manage.pyapp
from wsgiref.simple_server import make_server from urls import URLpattern def applications(environ, start_response): # 當前請求路徑 path = environ.get("PATH_INFO") print(path) start_response('200 OK', [('Content-Type', 'text/html'), ("Charset", "utf8")]) func = None for item in URLpattern: if path == item[0]: func = item[1] break if func: return [func(environ)] else: return [b"<h1>404!<h1>"] if __name__ == '__main__': server = make_server("127.0.0.1", 8889, applications) print("服務器開始啓動") server.serve_forever()
urls.py
URLpattern = ( ("/login", login), ("/favicon.ico", fav), )
views.py
import datetime import pymysql from urllib.parse import parse_qs def login(environ): with open("templates/login.html", "rb") as f: data = f.read() return data def index(environ): with open("templates/index.html", "rb") as f: data = f.read() return data def fav(environ): with open("templates/favicon.ico", "rb") as f: data = f.read() return data def reg(environ): with open("templates/reg.html", "rb") as f: data = f.read() return data def timer(environ): now = datetime.datetime.now().strftime("%y-%m-%d %x") return now.encode("utf8") def auth(request): try: request_body_size = int(request.get('CONTENT_LENGTH', 0)) except (ValueError): request_body_size = 0 request_body = request['wsgi.input'].read(request_body_size) data = parse_qs(request_body) user = data.get(b"user")[0].decode("utf-8") pwd = data.get(b"pwd")[0].decode("utf-8") # 鏈接數據庫 conn = pymysql.connect(host='10.1.2.71', port=3306, user='root', passwd='testjfz', db='blog') # 建立遊標 cur = conn.cursor() SQL="select * from userinfo where name='%s' and password='%s'" %(user,pwd) cur.execute(SQL) if cur.fetchone(): f = open("templates/backend.html", "rb") data = f.read() data = data.decode("utf8") return data.encode("utf8") else: print("OK456") return b"user or pwd is wrong"
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="http://127.0.0.1:8889/auth" method="post"> username:<input type="text" name="user"><br> password: <input type="password" name="pwd"><br> <input type="submit" name="btnsbt" value="commit"> </form> </body> </html>
backend.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h4>歡迎來到登陸頁面</h4> </body> </html>