Python實(shí)現(xiàn)手寫(xiě)一個(gè)類(lèi)似django的web框架示例
本文實(shí)例講述了Python實(shí)現(xiàn)手寫(xiě)一個(gè)類(lèi)似django的web框架。分享給大家供大家參考,具體如下:
用與django相似結(jié)構(gòu)寫(xiě)一個(gè)web框架。
啟動(dòng)文件代碼:
from wsgiref.simple_server import make_server #導(dǎo)入模塊 from views import * import urls def routers(): #這個(gè)函數(shù)是個(gè)元組 URLpattern=urls.URLpattern return URLpattern #這個(gè)函數(shù)執(zhí)行后返回這個(gè)元組 def application(environ,start_response): print("ok1") path=environ.get("PATH_INFO") print("path",path) start_response('200 OK',[('Content-Type','text/html')]) urlpattern=routers() #講函數(shù)的返回值元組賦值 func=None for item in urlpattern: #遍歷這個(gè)元組 if path==item[0]: #item[0]就是#路徑后面的斜杠內(nèi)容 func=item[1] #item[1]就是對(duì)應(yīng)的函數(shù)名 break if func: #如果路徑內(nèi)容存在函數(shù)就存在 return func(environ) #執(zhí)行這個(gè)函數(shù) else: print("ok5") return [b"404"] #如果不存在就返回404 if __name__=='__main__': print("ok0") t=make_server("",9700,application) print("ok22") t.serve_forever()
urls.py文件代碼:
from views import * URLpattern = ( ("/login", login), ("/alex", foo1), ("/egon", foo2), ("/auth", auth) )
views.py文件代碼:
def foo1(request): # 定義函數(shù) f=open("templates/alex.html","rb") #打開(kāi)html 以二進(jìn)制的模式 data=f.read() #讀到data里 f.close() #關(guān)閉 return [data] #返回這個(gè)data def foo2(request): f=open("templates/egon.html","rb") data=f.read() f.close() return [data] def login(request): f=open("templates/login.html","rb") data=f.read() f.close() return [data] def auth(request): print("+++",request) user_union,pwd_union=request.get("QUERY_STRING").split("&") _,user=user_union.split("=") _,pwd=pwd_union.split("=") if user=='Yuan' and pwd=="123": return [b"login,welcome"] else: return [b"user or pwd is wriong"]
templates目錄下的html文件:
alex.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> </head> <body> <div>alex</div> </body> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>登錄頁(yè)面</h2> <form action="http://127.0.0.1:9700/auth"> <p>姓名:<input type="text" name="user"></p> <p>密碼:<input type="password" name="pwd"></p> <p> <input type="submit"> </p> </form> </body> </html>
下面如圖,是目錄結(jié)構(gòu)
訪問(wèn)ip+prot+路徑 即為相應(yīng)的html,功能簡(jiǎn)單,只是為了熟悉django
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Python Socket編程技巧總結(jié)》、《Python URL操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python常用web框架簡(jiǎn)單性能測(cè)試結(jié)果分享(包含django、flask、bottle、tornado)
- Python Web框架之Django框架Model基礎(chǔ)詳解
- Python之Web框架Django項(xiàng)目搭建全過(guò)程
- 全面解讀Python Web開(kāi)發(fā)框架Django
- Python Web框架Flask下網(wǎng)站開(kāi)發(fā)入門(mén)實(shí)例
- Python Web框架Flask中使用七牛云存儲(chǔ)實(shí)例
- Python Web框架Flask中使用百度云存儲(chǔ)BCS實(shí)例
- Python Tornado框架輕松寫(xiě)一個(gè)Web應(yīng)用的全過(guò)程
- Python Web框架Tornado運(yùn)行和部署
- Python常用Web框架Django、Flask與Tornado介紹
相關(guān)文章
Python Multiprocessing多進(jìn)程 使用tqdm顯示進(jìn)度條的實(shí)現(xiàn)
這篇文章主要介紹了Python Multiprocessing多進(jìn)程 使用tqdm顯示進(jìn)度條的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08使用django-suit為django 1.7 admin后臺(tái)添加模板
前面我們介紹了Django-grappelli給admin添加模板,可是使用中發(fā)現(xiàn)inline有點(diǎn)問(wèn)題,所以就換了今天我們要談的Django-suit,貌似要稍微好一些2014-11-11Python讀取HDFS目錄下的所有文件的實(shí)現(xiàn)示例
HDFS是Apache Hadoop的分布式文件系統(tǒng),本文主要介紹了Python讀取HDFS目錄下的所有文件的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07python基礎(chǔ)之文件處理知識(shí)總結(jié)
今天帶大家了解python文件處理的相關(guān)知識(shí),文中介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05python析構(gòu)函數(shù)用法及注意事項(xiàng)
在本篇文章里小編給大家整理的是一篇關(guān)于python析構(gòu)函數(shù)用法及注意事項(xiàng),有需要的朋友們可以學(xué)習(xí)參考下。2021-06-06python實(shí)現(xiàn)ip地址查詢(xún)經(jīng)緯度定位詳解
這篇文章主要介紹了python實(shí)現(xiàn)ip地址查詢(xún)經(jīng)緯度定位詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08