溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-04-27 16:58:01 來源:億速云 閱讀:117 作者:Leah 欄目:編程語言

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

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()

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

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI