python實現WSGI的框架
python中實現WSGI的框架
1、說明
Application類對WSGI又做了一層簡單的封裝,由于上面說過WSGI函數返回的是一個可以迭代對象,所以需要實現一個__iter__方法,里面控制了客戶端的請求路由并且返回不同的輸出。
2、實例
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中實現WSGI的框架,希望對大家有所幫助。更多Python學習推薦:請關注IT培訓機構:千鋒教育。

相關推薦HOT
更多>>
pythonfor循環是什么
pythonfor循環是什么在做遍歷的時候,對于一些數據的反復循環執行,我們會用到for循環的語句。可以說這是新手入門必學的語句之一,在很多基礎循...詳情>>
2023-11-13 07:46:36
pythoncontextmanager()的轉換
python中contextmanager()的轉換1、說明當發出請求時,requests庫會在將請求實際發送到目標服務器之前準備該請求。請求準備包括像驗證頭信息和...詳情>>
2023-11-13 06:34:35
python使用items()遍歷鍵值對
python使用items()遍歷鍵值對字典可以用來存儲各種方式的信息,所以有很多方式可以通過字典的所有鍵值對、鍵或值。說明1、即使通過字典,鍵值對...詳情>>
2023-11-13 04:24:15
python實例方法中self的作用
python實例方法中self的作用說明1、無論是創建類的構造方法還是實例方法,最少要包含一個參數self。2、通過實例的self參數與對象進行綁定,程序...詳情>>
2023-11-13 03:46:48