python miniWeb框架搭建過程詳解
框架概念
框架和web服務(wù)器關(guān)系
·靜態(tài)資源:不是經(jīng)常變化的資源、往往是固定不變的資源
·動(dòng)態(tài)資源:經(jīng)常變化的資源
·模板文件:提供了一個(gè)顯示的模板,顯示的內(nèi)容不同,但是結(jié)構(gòu)是一樣的
·服務(wù)器的作用:
o1)接受客戶端請求
o2)響應(yīng)客戶端請求
o3)調(diào)用應(yīng)用框架獲取
miniWeb框架構(gòu)建基本構(gòu)建
·思路:
o判斷請求的資源路徑是 是否是 .py 結(jié)尾
o如果 .py 結(jié)尾,——> 顯示動(dòng)態(tài)內(nèi)容
o如果.html 結(jié)尾,——> 顯示靜態(tài)內(nèi)容
·核心代碼:
核心代碼: # index.py if file_path.endswith(".py"): # 2. 讓.py 顯示的內(nèi)容和.html顯示的內(nèi)容區(qū)別開開 response_body = "This is index Show! %s" % time.ctime() # 調(diào)用 utils 模塊的 create_http_response 函數(shù),拼接響應(yīng)協(xié)議 response_data = utils.create_http_response("200 OK", response_body.encode()) # index.html else: ....
miniWeb框架構(gòu)建-動(dòng)態(tài)顯示
·思路:
o首先必須是 .py 結(jié)尾的文件
o判斷請求的資源路徑,并且根據(jù)資源路徑不同設(shè)置 不同的 response_body
o當(dāng)請求的資源路徑不存在,返回 404 錯(cuò)誤
·核心代碼:
# 3. 判斷請求的資源路徑,根據(jù)不同的路徑顯示不同的額內(nèi)容 if file_path == "/index.py": response_body = "This is index show!" # 調(diào)用 utils 模塊的 create_http_response 函數(shù),拼接響應(yīng)協(xié)議 response_data = utils.create_http_response("200 OK", response_body.encode()) elif file_path == "/center.py": response_body = "This is center show!" # 調(diào)用 utils 模塊的 create_http_response 函數(shù),拼接響應(yīng)協(xié)議 response_data = utils.create_http_response("200 OK", response_body.encode()) elif file_path == "/gettime.py": response_body = "helloworld! %s" % time.ctime() # 調(diào)用 utils 模塊的 create_http_response 函數(shù),拼接響應(yīng)協(xié)議 response_data = utils.create_http_response("200 OK", response_body.encode()) else: response_body = "Sorry Page Not Found ! 404" # 調(diào)用 utils 模塊的 create_http_response 函數(shù),拼接響應(yīng)協(xié)議 response_data = utils.create_http_response("404 Not Found", response_body.encode())
路由列表(django)
·實(shí)現(xiàn)步驟:
o創(chuàng)建 urls 模塊,模塊的作用提供一個(gè)路由字典
字典保存路徑和函數(shù)的對應(yīng)關(guān)系
o導(dǎo)入函數(shù)的模塊 from application import funs
oroute_dict
定義路由字典
route_dict = { '/index.py': funs.index, '/center.py': funs.center, '/gettime.py': funs.gettime }
·創(chuàng)建 funs 模塊, 提供了具體的功能對應(yīng)的函數(shù)
定義路徑對應(yīng)的函數(shù)
import time def index(): """ 處理 index.py 請求 """ return "This is index show!--funs" def center(): """ 處理 index.py 請求 """ return "This is center show!" def gettime(): """ 處理 index.py 請求 """ return "This is gettime show! %s " % time.ctime()
·修改app文件中 動(dòng)態(tài)顯示的判斷部分
1.判斷 路徑 是否在 路由字典中 key in 字典
2.如果在字典中,根據(jù)key(請求路徑) 取出 對應(yīng)的函數(shù)的引用
3.執(zhí)行函數(shù),獲取函數(shù)的返回值,然后賦值 給 response_body
if file_path in urls.route_dict: # 根據(jù)key值,去urls.route_dict中,獲取值(函數(shù)引用) func = urls.route_dict[file_path] # 根據(jù)路由字典,獲取函數(shù)的引用,執(zhí)行該函數(shù),返回執(zhí)行的結(jié)果, # 保存到 response_body 變量中 response_body = func() # 調(diào)用 utils 模塊的 create_http_response 函數(shù),拼接響應(yīng)協(xié)議 response_data = utils.create_http_response("200 OK", response_body.encode()) else:
裝飾器路由(flask)
使用裝飾器工廠,實(shí)現(xiàn)裝飾器路由
·修改urls模塊
route_dict = { }
·修改funs模塊
o導(dǎo)入 from application import urls
o創(chuàng)建裝飾器工廠,并且把路徑添加到字典中(創(chuàng)建路由字典)
def route(path): # path 向裝飾器內(nèi)部傳遞的參數(shù)path/index.py # 裝飾器 # 字典 # {"index.py":index函數(shù)引用} def function_out(func): #funcindex函數(shù)的引用 # 2----- urls.route_dict[path] = func # print("裝飾[%s]" % path) # 裝飾器內(nèi)層函數(shù) def function_in(): # 調(diào)用原函數(shù)并且執(zhí)行 return func() return function_in return function_out
o裝飾函數(shù)
@route("/center.py") def center(): """ 處理 index.py 請求 """ return "This is center show!"
o在 app模塊中導(dǎo)入 funs 模塊
此時(shí)funs 模塊中的函數(shù)被加載,加載的同時(shí)被裝飾(就會(huì)向字典中添加路由信息)
模板替換
·思路
o拷貝資源(templates)到工程下
o修改 funs模塊中的 index 和 center函數(shù)
o在函數(shù)中讀取對應(yīng)的文件
List item
o使用正則替換網(wǎng)頁中的內(nèi)容 {%content%} —> helloworld!
o返回替換后的內(nèi)容
with open("templates/index.html") as file:? content = file.read() return content
數(shù)據(jù)庫操作
數(shù)據(jù)加載
·創(chuàng)建并導(dǎo)入數(shù)據(jù)到數(shù)據(jù)庫
o創(chuàng)建數(shù)據(jù)庫 create database stock_db charset=utf8
o使用數(shù)據(jù)庫 use stock_db
o導(dǎo)入數(shù)據(jù)庫(先客戶端登錄)
o準(zhǔn)備腳本文件
o導(dǎo)入腳本 source stock_db.sql
·修改index函數(shù)
o連接數(shù)據(jù)庫,獲取數(shù)據(jù)
§導(dǎo)入模塊
§建立連接
§創(chuàng)建游標(biāo)
§使用游標(biāo)執(zhí)行sql
§獲取查詢的結(jié)果
data_from_mysql = str(cur.fetchall())
§關(guān)閉資源
先關(guān)閉游標(biāo),在關(guān)閉連接
§替換為查詢的數(shù)據(jù)
content = re.sub("{%content%}",data_from_mysql,content)
渲染頁面
·思路:
o把查詢的數(shù)據(jù)進(jìn)行遍歷,并且拼接html格式的文本
o表示一行 一列
o替換為拼接后的字符串
content = re.sub("{%content%}",data_from_myql,content)
o注意:
%s %s %s —> line # line 是一個(gè)元組
多表查詢
·思路:
o關(guān)聯(lián)查詢
select i.code,i.short,i.chg,i.turnover,i.price,i.highs,f.note_info from info i, focus f where i.id = f.id
o把查詢的數(shù)據(jù)進(jìn)行遍歷,并且拼接html格式的文本
o表示一行 一列
o替換為拼接后的字符串
content = re.sub("{%content%}",data_from_myql,content)
多進(jìn)程版
·設(shè)置進(jìn)程守護(hù)
p1.daemon = True
·啟動(dòng)進(jìn)程
p1.start()
·關(guān)閉new_client_socket ,否則無法釋放套接字
new_client_socket.close()
到此這篇關(guān)于python miniWeb框架搭建的文章就介紹到這了,更多相關(guān)python miniWeb框架搭建內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。