Django應(yīng)用程序入口WSGIHandler源碼解析
前言
WSGI 有三個(gè)部分, 分別為服務(wù)器(server), 應(yīng)用程序(application) 和中間件(middleware). 已經(jīng)知道, 服務(wù)器方面會(huì)調(diào)用應(yīng)用程序來(lái)處理請(qǐng)求, 在應(yīng)用程序中有真正的處理邏輯, 在這里面幾乎可以做任何事情, 其中的中間件就會(huì)在里面展開.
Django 中的應(yīng)用程序
任何的 WSGI 應(yīng)用程序, 都必須是一個(gè) start_response(status, response_headers, exc_info=None) 形式的函數(shù)或者定義了 __call__ 的類. 而 django.core.handlers 就用后一種方式實(shí)現(xiàn)了應(yīng)用程序: WSGIHandler. 在這之前, Django 是如何指定自己的 application 的, 在一個(gè)具體的 Django 項(xiàng)目中, 它的方式如下:
在 mysite.settings.py 中能找到如下設(shè)置:
# Python dotted path to the WSGI application used by Django's runserver. WSGI_APPLICATION = 'tomato.wsgi.application'
如你所見, WSGI_APPLICATION 就指定了應(yīng)用程序. 而按圖索驥下去, 找到項(xiàng)目中的 wsgi.py, 已經(jīng)除去了所有的注釋:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tomato.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
因此, WSGI_APPLICATION 所指定的即為 wsgi.py 中的全局變量 application. 故伎重演, 繼續(xù)找下去. 在 django.core 模塊中的 wsgi.py 中找到 get_wsgi_application() 函數(shù)的實(shí)現(xiàn):
from django.core.handlers.wsgi import WSGIHandler def get_wsgi_application(): """ The public interface to Django's WSGI support. Should return a WSGI callable. Allows us to avoid making django.core.handlers.WSGIHandler public API, in case the internal WSGI implementation changes or moves in the future. """ """ # 繼承, 但只實(shí)現(xiàn)了 __call__ 方法, 方便使用 class WSGIHandler(base.BaseHandler): """ return WSGIHandler()
在 get_wsgi_application() 中實(shí)例化了 WSGIHandler, 并無(wú)其他操作.
WSGIHandler
緊接著在 django.core.handler 的 base.py 中找到 WSGIHandler 的實(shí)現(xiàn).
# 繼承, 但只實(shí)現(xiàn)了 __call__ 方法, 方便使用
class WSGIHandler(base.BaseHandler):
initLock = Lock()
# 關(guān)于此, 日后展開, 可以將其視為一個(gè)代表 http 請(qǐng)求的類
request_class = WSGIRequest
# WSGIHandler 也可以作為函數(shù)來(lái)調(diào)用
def __call__(self, environ, start_response):
# Set up middleware if needed. We couldn't do this earlier, because
# settings weren't available.
# 這里的檢測(cè): 因?yàn)?self._request_middleware 是最后才設(shè)定的, 所以如果為空,
# 很可能是因?yàn)?self.load_middleware() 沒有調(diào)用成功.
if self._request_middleware is None:
with self.initLock:
try:
# Check that middleware is still uninitialised.
if self._request_middleware is None:
因?yàn)?load_middleware() 可能沒有調(diào)用, 調(diào)用一次.
self.load_middleware()
except:
# Unload whatever middleware we got
self._request_middleware = None
raise
set_script_prefix(base.get_script_name(environ))
signls.request_started.send(sender=self.__class__) # __class__ 代表自己的類
try:
# 實(shí)例化 request_class = WSGIRequest, 將在日后文章中展開, 可以將其視為一個(gè)代表 http 請(qǐng)求的類
request = self.request_class(environ)
except UnicodeDecodeError:
logger.warning('Bad Request (UnicodeDecodeError)',
exc_info=sys.exc_info(),
extra={
'status_code': 400,
}
)
response = http.HttpResponseBadRequest()
else:
# 調(diào)用 self.get_response(), 將會(huì)返回一個(gè)相應(yīng)對(duì)象 response<br> ############# 關(guān)鍵的操作, self.response() 可以獲取響應(yīng)數(shù)據(jù).
response = self.get_response(request)
# 將 self 掛鉤到 response 對(duì)象
response._handler_class = self.__class__
try:
status_text = STATUS_CODE_TEXT[response.status_code]
except KeyError:
status_text = 'UNKNOWN STATUS CODE'
# 狀態(tài)碼
status = '%s %s' % (response.status_code, status_text)
response_headers = [(str(k), str(v)) for k, v in response.items()]
# 對(duì)于每個(gè)一個(gè) cookie, 都在 header 中設(shè)置: Set-cookie xxx=yyy
for c in response.cookies.values():
response_headers.append((str('Set-Cookie'), str(c.output(header=''))))
# start_response() 操作已經(jīng)在上節(jié)中介紹了
start_response(force_str(status), response_headers)
# 成功返回相應(yīng)對(duì)象
return response
WSGIHandler 類只實(shí)現(xiàn)了 def __call__(self, environ, start_response), 使它本身能夠成為 WSGI 中的應(yīng)用程序, 并且實(shí)現(xiàn) __call__ 能讓類的行為跟函數(shù)一樣, 詳見 python __call__ 方法.
def __call__(self, environ, start_response) 方法中調(diào)用了 WSGIHandler.get_response() 方法以獲取響應(yīng)數(shù)據(jù)對(duì)象 response. 從 WSGIHandler 的實(shí)現(xiàn)來(lái)看, 它并不是最為底層的: WSGIHandler 繼承自 base.BaseHandler, 在 django.core.handler 的 base.py 中可以找到: class BaseHandler(object):...
這一節(jié)服務(wù)器部分已經(jīng)結(jié)束, 接下來(lái)的便是中間件和應(yīng)用程序了, 相關(guān)內(nèi)容會(huì)在下節(jié)的 BaseHandler 中展開. 我已經(jīng)在 github 備份了 Django 源碼的注釋: Decode-Django, 有興趣的童鞋 fork 吧.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python快速打開一個(gè)百萬(wàn)行級(jí)別的超大Excel文件的方法
這篇文章主要介紹了使用Python快速打開一個(gè)百萬(wàn)行級(jí)別的超大Excel文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
python實(shí)現(xiàn)12306搶票及自動(dòng)郵件發(fā)送提醒付款功能
本文給大家分享python實(shí)現(xiàn)12306搶票及自動(dòng)郵件發(fā)送提醒付款功能,文章沒有給大家貼出完整代碼,只是技術(shù)交流,感興趣的朋友跟隨腳本之家小編一起看看吧2018-03-03
Pandas中數(shù)據(jù)表合并的幾種實(shí)現(xiàn)方法
Pandas提供了merge()、concat()和join()三種方法來(lái)合并數(shù)據(jù)表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Python檢測(cè)網(wǎng)絡(luò)延遲的代碼
這篇文章主要介紹了Python檢測(cè)網(wǎng)絡(luò)延遲的代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

