WSGI
Web Server Gateway Interface,是一種協議規範. // 描述服務器怎麼和應用程序通訊
組成:
web server和application // web服務器和應用app
**
web server 負責接受客戶端的請求,調用WSGI應用程序,傳入environ(請求對象),start_response(負責返回響應)
application 負責處理請求,返回響應給服務器
**
注意:
wsgi要求application:
a> 應用程序是一個可調用對象 // 函數、類(實現__call__)
b> 應用程序必須接受2個參數 // environ和start_response
c> 應用程序必須返回可迭代的值
app.run運行原理
app.run --> werkzeug.serving.run_simple --> BaseWSGIServer--> HTTPServer --> socketserver.TCPServer --> socketserver.BaseServer
app.run()
run_simple(host, port, self, **options) // 啓動服務器,內部會加載中間過程 判斷static和debug等
__call__(self, environ, start_response) // 服務器調用應用程序,觸發__call__
wsgi_app(self, environ, start_response) // 真正的wsgi處理程序