對於真實開發中的python web程序來說,一般會分為兩部分:服務器程序和應用程序。
服務器程序負責對socket服務端進行封裝,並在請求到來時,對請求的各種數據進行整理。
應用程序則負責具體的邏輯處理。為了方便應用程序的開發,就出現了眾多的web框架,例如:Django、Flask、web.py等。不同的框架有不同的開發方式,但是如論如何,開發出的應用程序都要和服務器程序配合,才能為用戶提供服務。
這樣,服務器程序就需要為不同的框架提供不同的支持,這種混亂的局面無論對於服務器還是框架,都是不好的。對於服務器來說,需要支持不同的框架;對框架來說,只有支持它的服務器才能別開發出的應用使用。
這時候標准化就變得非常重要,我們可以設定一個標准,只要服務器程序支持這個標准,框架也支持這個標准,那么他們就可以配合使用。一旦標准確定,雙方各自實現。這樣,服務器就可以支持更多支持標准的框架,框架也可以使用更多支持標准的服務器。
WSGI(Web Server Gateway Interface)就是一種規范,它定義了使用Python編寫的web應用程序與web服務器程序之間的接口格式,顯示web應用程序與web服務器程序間的解耦。
常用的WSGI服務器有uwsgi,Gunicom。而Python標准庫提供的獨立WSGI服務器叫wsgiref, Django開發環境用的就是這個模塊來做服務器。
1. wsgiref
使用wsgiref模塊來替換自己寫的web框剪的socket server部分:
1 from wsgiref.simple_server import make_server 2 #將返回不同的內容部分封裝成函數 3 def index(url): 4 # 讀取index.html頁面的內容 5 with open("index.html", "r", encoding="utf8") as f: 6 s = f.read() 7 # 返回字節數據 8 return bytes(s, encoding="utf8") 9 10 11 def home(url): 12 with open("home.html", "r", encoding="utf8") as f: 13 s = f.read() 14 return bytes(s, encoding="utf8") 15 16 17 def timer(url): 18 import time 19 with open("time.html", "r", encoding="utf8") as f: 20 s = f.read() 21 s = s.replace('@@time@@', time.strftime("%Y-%m-%d %H:%M:%S")) 22 return bytes(s, encoding="utf8") 23 24 25 # 定義一個url和實際要執行的函數的對應關系 26 list1 = [ 27 ("/index/", index), 28 ("/home/", home), 29 ("/time/", timer), 30 ] 31 32 33 def run_server(environ, start_response): 34 start_response('200 OK', [('Content-Type', 'text/html;charset=utf8'), ]) # 設置HTTP響應的狀態碼和頭信息 35 url = environ['PATH_INFO'] # 取到用戶輸入的url 36 func = None 37 for i in list1: 38 if i[0] == url: 39 func = i[1] 40 break 41 if func: 42 response = func(url) 43 else: 44 response = b"404 not found!" 45 return [response, ] 46 47 48 if __name__ == '__main__': 49 httpd = make_server('127.0.0.1', 8090, run_server) 50 httpd.serve_forever()
2. jinja2
上面的代碼只是實現了一個簡單的動態,完全可以從數據庫中查詢數據,然后去替換html中對應的內容,然后再發送給瀏覽器來完成渲染。
這個過程就相當於HTML模板渲染數據。
本質上就是HTML內容中利用一些特殊的符號來替換要展示的數據。
下載jinja2的命令:
pip3 install jinja2
2.1 使用jinja2渲染index.html文件
首先建立一個名稱為index.html的文件,代碼如下:
1 <!DOCTYPE html> 2 <html lang="zh-CN"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta http-equiv="x-ua-compatible" content="IE=edge"> 6 <meta name="viewport" content="width=device-width, initial-scale=1"> 7 <title>Title</title> 8 </head> 9 <body> 10 <h1>姓名:{{name}}</h1> 11 <h1>愛好:</h1> 12 <ul> 13 {% for hobby in hobby_list %} 14 <li>{{hobby}}</li> 15 {% endfor %} 16 </ul> 17 </body> 18 </html>
使用jinja2渲染index.html文件代碼如下:
1 from wsgiref.simple_server import make_server 2 from jinja2 import Template 3 4 5 def index(url): 6 # 讀取HTML文件內容 7 with open("index2.html", "r", encoding="utf8") as f: 8 data = f.read() 9 template = Template(data) # 生成模板文件 10 ret = template.render({'name': 'yang', 'hobby_list': ['讀書', '運動', '寫代碼']}) # 把數據填充到模板中 11 return bytes(ret, encoding="utf8") 12 13 14 def home(url): 15 with open("home.html", "r", encoding="utf8") as f: 16 s = f.read() 17 return bytes(s, encoding="utf8") 18 19 20 # 定義一個url和實際要執行的函數的對應關系 21 list1 = [ 22 ("/index/", index), 23 ("/home/", home), 24 ] 25 26 27 def run_server(environ, start_response): 28 start_response('200 OK', [('Content-Type', 'text/html;charset=utf8'), ]) # 設置HTTP響應的狀態碼和頭信息 29 url = environ['PATH_INFO'] # 取到用戶輸入的url 30 func = None 31 for i in list1: 32 if i[0] == url: 33 func = i[1] 34 break 35 if func: 36 response = func(url) 37 else: 38 response = b"404 not found!" 39 return [response, ] 40 41 42 if __name__ == '__main__': 43 httpd = make_server('127.0.0.1', 8090, run_server) 44 print("我在8090等你哦...") 45 httpd.serve_forever()
現在頁面中的數據都是我們自己手寫的,但是還可以從數據庫查詢數據,來填充頁面。
使用pymysql連接數據庫:
1 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="xxx", db="xxx", charset="utf8") 2 cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) 3 cursor.execute("select name, age, department_id from userinfo") 4 user_list = cursor.fetchall() 5 cursor.close() 6 conn.close()
創建一個測試的user表:
1 CREATE TABLE user( 2 id int auto_increment PRIMARY KEY, 3 name CHAR(10) NOT NULL, 4 hobby CHAR(20) NOT NULL 5 )engine=innodb DEFAULT charset=UTF8;
模板的原理就是字符串替換,只要在HTML頁面中遵循jinja2的語法規則編寫,其內部就會按照指定的語法進行相應的替換,從而達到動態的返回內容。
