溫馨提示×

溫馨提示×

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

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

Django應用程序入口WSGIHandler源碼解析

發(fā)布時間:2020-10-21 16:17:07 來源:腳本之家 閱讀:154 作者:搗亂小子 欄目:開發(fā)技術(shù)

前言

WSGI 有三個部分, 分別為服務器(server), 應用程序(application) 和中間件(middleware). 已經(jīng)知道, 服務器方面會調(diào)用應用程序來處理請求, 在應用程序中有真正的處理邏輯, 在這里面幾乎可以做任何事情, 其中的中間件就會在里面展開.

Django 中的應用程序

任何的 WSGI 應用程序, 都必須是一個 start_response(status, response_headers, exc_info=None) 形式的函數(shù)或者定義了 __call__ 的類. 而 django.core.handlers 就用后一種方式實現(xiàn)了應用程序: WSGIHandler. 在這之前, Django 是如何指定自己的 application 的, 在一個具體的 Django 項目中, 它的方式如下:

在 mysite.settings.py 中能找到如下設置:

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'tomato.wsgi.application'

如你所見, WSGI_APPLICATION 就指定了應用程序. 而按圖索驥下去, 找到項目中的 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ù)的實現(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.
 
  """
  """
  # 繼承, 但只實現(xiàn)了 __call__ 方法, 方便使用
  class WSGIHandler(base.BaseHandler):
  """
  return WSGIHandler()

在 get_wsgi_application() 中實例化了 WSGIHandler, 并無其他操作.

WSGIHandler

緊接著在 django.core.handler 的 base.py 中找到 WSGIHandler 的實現(xiàn).

# 繼承, 但只實現(xiàn)了 __call__ 方法, 方便使用
class WSGIHandler(base.BaseHandler):
  initLock = Lock() 
  # 關(guān)于此, 日后展開, 可以將其視為一個代表 http 請求的類
  request_class = WSGIRequest 
  # WSGIHandler 也可以作為函數(shù)來調(diào)用
  def __call__(self, environ, start_response):
    # Set up middleware if needed. We couldn't do this earlier, because
    # settings weren't available. 
    # 這里的檢測: 因為 self._request_middleware 是最后才設定的, 所以如果為空,
    # 很可能是因為 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:
            因為 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:
      # 實例化 request_class = WSGIRequest, 將在日后文章中展開, 可以將其視為一個代表 http 請求的類
      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(), 將會返回一個相應對象 response<br>      ############# 關(guān)鍵的操作, self.response() 可以獲取響應數(shù)據(jù).     
      response = self.get_response(request)
 
    # 將 self 掛鉤到 response 對象
    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()] 
    # 對于每個一個 cookie, 都在 header 中設置: 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) 
    # 成功返回相應對象
    return response

WSGIHandler 類只實現(xiàn)了 def __call__(self, environ, start_response), 使它本身能夠成為 WSGI 中的應用程序, 并且實現(xiàn) __call__ 能讓類的行為跟函數(shù)一樣, 詳見 python __call__ 方法.

def __call__(self, environ, start_response) 方法中調(diào)用了 WSGIHandler.get_response() 方法以獲取響應數(shù)據(jù)對象 response. 從 WSGIHandler 的實現(xiàn)來看, 它并不是最為底層的: WSGIHandler 繼承自 base.BaseHandler, 在 django.core.handler 的 base.py 中可以找到: class BaseHandler(object):...

這一節(jié)服務器部分已經(jīng)結(jié)束, 接下來的便是中間件和應用程序了, 相關(guān)內(nèi)容會在下節(jié)的 BaseHandler 中展開. 我已經(jīng)在 github 備份了 Django 源碼的注釋: Decode-Django, 有興趣的童鞋 fork 吧.

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

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

AI