怎么在Python中實現(xiàn)一個WSGI框架

怎么在Python中實現(xiàn)一個WSGI框架?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

創(chuàng)新互聯(lián)建站主營米林網站建設的網絡公司,主營網站建設方案,App定制開發(fā),米林h5微信平臺小程序開發(fā)搭建,米林網站營銷推廣歡迎米林等地區(qū)企業(yè)咨詢

python有哪些常用庫

python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

1、說明

Application類對WSGI又做了一層簡單的封裝,由于上面說過WSGI函數(shù)返回的是一個可以迭代對象,所以需要實現(xiàn)一個__iter__方法,里面控制了客戶端的請求路由并且返回不同的輸出。

2、實例

from wsgiref.simple_server import make_server
 
class Application(object):
 
    def __init__(self, environ, start_response):
        self.start_response = start_response
        self.path = environ['PATH_INFO']
 
    def __iter__(self):
        if self.path == '/':
            status = '200 OK'
            response_headers = [('Content-type', 'text/html')]
            self.start_response(status, esponse_headers)
            yield '<h2>Hello,World!</h2>'.encode('utf-8')
 
        elif self.path == '/wsgi':
            status = '200 OK'
            response_headers = [('Content-type', 'text/html')]
            self.start_response(status, response_headers)
            yield '<h2>Hello,WSGI!</h2>'.encode('utf-8')
 
        else:
            status = '404 NOT FOUND'
            response_headers = [('Content-type', 'text/html')]
            self.start_response(status, response_headers)
            yield '<h2>404 NOT FOUND</h2>'.encode('utf-8')
 
if __name__ == "__main__":
    app = make_server('127.0.0.1', 8000, Application)
    print('Serving HTTP on port 8000...')
    app.serve_forever()

看完上述內容,你們掌握怎么在Python中實現(xiàn)一個WSGI框架的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

名稱欄目:怎么在Python中實現(xiàn)一個WSGI框架
文章源于:http://www.muchs.cn/article2/gcepoc.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、標簽優(yōu)化關鍵詞優(yōu)化、虛擬主機、網站收錄、電子商務

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

外貿網站建設