python miniWeb框架搭建過程詳解
框架概念
框架和web服務(wù)器關(guān)系
·靜態(tài)資源:不是經(jīng)常變化的資源、往往是固定不變的資源
·動(dòng)態(tài)資源:經(jīng)常變化的資源
·模板文件:提供了一個(gè)顯示的模板,顯示的內(nèi)容不同,但是結(jié)構(gòu)是一樣的
·服務(wù)器的作用:
o1)接受客戶端請(qǐng)求
o2)響應(yīng)客戶端請(qǐng)求
o3)調(diào)用應(yīng)用框架獲取
miniWeb框架構(gòu)建基本構(gòu)建
·思路:
o判斷請(qǐng)求的資源路徑是 是否是 .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判斷請(qǐng)求的資源路徑,并且根據(jù)資源路徑不同設(shè)置 不同的 response_body
o當(dāng)請(qǐng)求的資源路徑不存在,返回 404 錯(cuò)誤
·核心代碼:
# 3. 判斷請(qǐng)求的資源路徑,根據(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ù)的對(duì)應(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 模塊, 提供了具體的功能對(duì)應(yīng)的函數(shù)
定義路徑對(duì)應(yīng)的函數(shù)
import time def index(): """ 處理 index.py 請(qǐng)求 """ return "This is index show!--funs" def center(): """ 處理 index.py 請(qǐng)求 """ return "This is center show!" def gettime(): """ 處理 index.py 請(qǐng)求 """ return "This is gettime show! %s " % time.ctime()
·修改app文件中 動(dòng)態(tài)顯示的判斷部分
1.判斷 路徑 是否在 路由字典中 key in 字典
2.如果在字典中,根據(jù)key(請(qǐng)求路徑) 取出 對(duì)應(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): #func index函數(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 請(qǐng)求 """ return "This is center show!"
o在 app模塊中導(dǎo)入 funs 模塊
此時(shí)funs 模塊中的函數(shù)被加載,加載的同時(shí)被裝飾(就會(huì)向字典中添加路由信息)
模板替換
·思路
o拷貝資源(templates)到工程下
o修改 funs模塊中的 index 和 center函數(shù)
o在函數(shù)中讀取對(duì)應(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在python中使用pymysql往mysql數(shù)據(jù)庫中插入(insert)數(shù)據(jù)實(shí)例
今天小編就為大家分享一篇在python中使用pymysql往mysql數(shù)據(jù)庫中插入(insert)數(shù)據(jù)實(shí)例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Pytorch搭建YoloV5目標(biāo)檢測(cè)平臺(tái)實(shí)現(xiàn)過程
這篇文章主要為大家介紹了Pytorch搭建YoloV5目標(biāo)檢測(cè)平臺(tái)實(shí)現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04Python GUI編程學(xué)習(xí)筆記之tkinter事件綁定操作詳解
這篇文章主要介紹了Python GUI編程學(xué)習(xí)筆記之tkinter事件綁定操作,結(jié)合實(shí)例形式分析了Python GUI編程tkinter事件綁定常見操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-03-03Django中的JWT身份驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了Django中的JWT身份驗(yàn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05pytorch 實(shí)現(xiàn)打印模型的參數(shù)值
今天小編就為大家分享一篇pytorch 實(shí)現(xiàn)打印模型的參數(shù)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12Python反爬機(jī)制-驗(yàn)證碼功能的具體實(shí)現(xiàn)過程
Tesseract-OCR是一個(gè)免費(fèi)、開源的OCR引擎,通過該引擎可以識(shí)別圖片中的驗(yàn)證碼,這篇文章主要介紹了Python反爬機(jī)制-驗(yàn)證碼的示例代碼,需要的朋友可以參考下2022-02-02手把手教你如何用Pycharm2020.1.1配置遠(yuǎn)程連接的詳細(xì)步驟
這篇文章主要介紹了如何用Pycharm2020.1.1配置遠(yuǎn)程連接,分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-08-08