python實(shí)現(xiàn)WSGI的框架
python中實(shí)現(xiàn)WSGI的框架
1、說(shuō)明
Application類對(duì)WSGI又做了一層簡(jiǎn)單的封裝,由于上面說(shuō)過(guò)WSGI函數(shù)返回的是一個(gè)可以迭代對(duì)象,所以需要實(shí)現(xiàn)一個(gè)__iter__方法,里面控制了客戶端的請(qǐng)求路由并且返回不同的輸出。
2、實(shí)例
fromwsgiref.simple_serverimportmake_server
classApplication(object):
def__init__(self,environ,start_response):
self.start_response=start_response
self.path=environ['PATH_INFO']
def__iter__(self):
ifself.path=='/':
status='200OK'
response_headers=[('Content-type','text/html')]
self.start_response(status,esponse_headers)
yield'
Hello,World!
'.encode('utf-8')
elifself.path=='/wsgi':
status='200OK'
response_headers=[('Content-type','text/html')]
self.start_response(status,response_headers)
yield'
Hello,WSGI!
'.encode('utf-8')
else:
status='404NOTFOUND'
response_headers=[('Content-type','text/html')]
self.start_response(status,response_headers)
yield'
404NOTFOUND
'.encode('utf-8')
if__name__=="__main__":
app=make_server('127.0.0.1',8000,Application)
print('ServingHTTPonport8000...')
app.serve_forever()
以上就是Python中實(shí)現(xiàn)WSGI的框架,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)推薦:請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。

猜你喜歡LIKE
相關(guān)推薦HOT
更多>>
pythonfor循環(huán)是什么
pythonfor循環(huán)是什么在做遍歷的時(shí)候,對(duì)于一些數(shù)據(jù)的反復(fù)循環(huán)執(zhí)行,我們會(huì)用到for循環(huán)的語(yǔ)句。可以說(shuō)這是新手入門必學(xué)的語(yǔ)句之一,在很多基礎(chǔ)循...詳情>>
2023-11-13 07:46:36
pythoncontextmanager()的轉(zhuǎn)換
python中contextmanager()的轉(zhuǎn)換1、說(shuō)明當(dāng)發(fā)出請(qǐng)求時(shí),requests庫(kù)會(huì)在將請(qǐng)求實(shí)際發(fā)送到目標(biāo)服務(wù)器之前準(zhǔn)備該請(qǐng)求。請(qǐng)求準(zhǔn)備包括像驗(yàn)證頭信息和...詳情>>
2023-11-13 06:34:35
python使用items()遍歷鍵值對(duì)
python使用items()遍歷鍵值對(duì)字典可以用來(lái)存儲(chǔ)各種方式的信息,所以有很多方式可以通過(guò)字典的所有鍵值對(duì)、鍵或值。說(shuō)明1、即使通過(guò)字典,鍵值對(duì)...詳情>>
2023-11-13 04:24:15
python實(shí)例方法中self的作用
python實(shí)例方法中self的作用說(shuō)明1、無(wú)論是創(chuàng)建類的構(gòu)造方法還是實(shí)例方法,最少要包含一個(gè)參數(shù)self。2、通過(guò)實(shí)例的self參數(shù)與對(duì)象進(jìn)行綁定,程序...詳情>>
2023-11-13 03:46:48熱門推薦
python實(shí)現(xiàn)WSGI的框架
沸pythonfor循環(huán)是什么
熱python-=是什么意思
熱python打開(kāi)文本文件有哪些方法?
新pythoncontextmanager()的轉(zhuǎn)換
pythonre是什么?
pythondecimal是什么
python列表追加元素出錯(cuò)的解決
python使用loguru操作日志
python使用items()遍歷鍵值對(duì)
pythonvim中有哪些對(duì)象
python實(shí)例方法中self的作用
pythonin和is的區(qū)分
pythonos.path.join()函數(shù)的使用
技術(shù)干貨







快速通道 更多>>
-
課程介紹
點(diǎn)擊獲取大綱 -
就業(yè)前景
查看就業(yè)薪資 -
學(xué)習(xí)費(fèi)用
了解課程價(jià)格 -
優(yōu)惠活動(dòng)
領(lǐng)取優(yōu)惠券 -
學(xué)習(xí)資源
領(lǐng)3000G教程 -
師資團(tuán)隊(duì)
了解師資團(tuán)隊(duì) -
實(shí)戰(zhàn)項(xiàng)目
獲取項(xiàng)目源碼 -
開(kāi)班地區(qū)
查看來(lái)校路線