詳細解讀Python的web.py架構下的application.py模組

來源:互聯網
上載者:User
本文主要分析的是web.py庫的application.py這個模組中的代碼。總的來說,這個模組主要實現了WSGI相容的介面,以便應用程式能夠被WSGI應用伺服器調用。WSGI是Web Server Gateway Interface的縮寫,具體細節可以查看WSGI的WIKI頁面
介面的使用
使用web.py內建的HTTP Server

下面這個例子來自官方文檔的Hello World,這個代碼一般是應用入口的代碼:

import weburls = ("/.*", "hello")app = web.application(urls, globals())class hello:  def GET(self):    return 'Hello, world!'if __name__ == "__main__":  app.run()

上面的例子描述了一個web.py應用最基本的組成元素:

  • URL路由表
  • 一個web.application執行個體app
  • 調用app.run()

其中,app.run()的調用是初始化各種WCGI介面,並啟動一個內建的HTTP伺服器和這些介面對接,代碼如下:

def run(self, *middleware):  return wsgi.runwsgi(self.wsgifunc(*middleware))

與WSGI應用伺服器對接

如果你的應用要與WSGI應用伺服器對接,比如uWSGI,gunicorn等,那麼應用入口的代碼就要換一種寫法了:

import webclass hello:  def GET(self):    return 'Hello, world!'urls = ("/.*", "hello")app = web.application(urls, globals())application = app.wsgifunc()

在這種情境下,應用的代碼不需要啟動HTTP伺服器,而是實現一個WSGI相容的介面供WSGI伺服器調用。web.py架構為我們實現了這樣的介面,你只需要調用application = app.wsgifunc()就可以了,這裡所得到的application變數就是WSGI介面(後面分析完代碼你就會知道了)。
WSGI介面的實現分析

分析主要圍繞著下面兩行代碼進行:

app = web.application(urls, globals())application = app.wsgifunc()

web.application執行個體化

初始化這個執行個體需要傳遞兩個參數:URL路由元組和globals()的結果。

另外,還可以傳遞第三個變數:autoreload,用來指定是否需要自動重新匯入Python模組,這在調試的時候很有用,不過我們分析主要過程的時候可以忽略。

application類的初始化代碼如下:

class application:  def __init__(self, mapping=(), fvars={}, autoreload=None):    if autoreload is None:      autoreload = web.config.get('debug', False)    self.init_mapping(mapping)    self.fvars = fvars    self.processors = []    self.add_processor(loadhook(self._load))    self.add_processor(unloadhook(self._unload))    if autoreload:      ...

其中,autoreload相關功能的代碼略去了。其他的代碼主要作了如下幾個事情:

  • self.init_mapping(mapping):初始化URL路由映射關係。
  • self.add_processor():添加了兩個處理器。

初始化URL路由映射關係

def init_mapping(self, mapping):  self.mapping = list(utils.group(mapping, 2))

這個函數還調用了一個工具函數,效果是這樣的:

urls = ("/", "Index",    "/hello/(.*)", "Hello",    "/world", "World")

如果使用者初始化時傳遞的元組是這樣的,那麼調用init_mapping之後:

self.mapping = [["/", "Index"],        ["/hello/(.*)", "Hello"],        ["/world", "World"]]

後面架構在進行URL路由時,就會遍曆這個列表。
添加處理器

  self.add_processor(loadhook(self._load))  self.add_processor(unloadhook(self._unload))

這兩行代碼添加了兩個處理器:self._load和self._unload,而且還對這兩個函數進行了裝飾。處理器的是用在HTTP請求處理前後的,它不是真正用來處理一個HTTP請求,但是可以用來作一些額外的工作,比如官方教程裡面有提到的給子應用添加session的做法,就是使用了處理器:

def session_hook():  web.ctx.session = sessionapp.add_processor(web.loadhook(session_hook))

處理器的定義和使用都是比較複雜的,後面專門講。
wsgifunc函數

wsgifunc的執行結果是返回一個WSGI相容的函數,並且該函數內部實現了URL路由等功能。

def wsgifunc(self, *middleware):  """Returns a WSGI-compatible function for this application."""  ...  for m in middleware:     wsgi = m(wsgi)  return wsgi

除開內建函式的定義,wsgifunc的定義就是這麼簡單,如果沒有實現任何中介軟體,那麼就是直接返回其內部定義的wsgi函數。
wsgi函數

該函數實現了WSGI相容介面,同時也實現了URL路由等功能。

def wsgi(env, start_resp):  # clear threadlocal to avoid inteference of previous requests  self._cleanup()  self.load(env)  try:    # allow uppercase methods only    if web.ctx.method.upper() != web.ctx.method:      raise web.nomethod()    result = self.handle_with_processors()    if is_generator(result):      result = peep(result)    else:      result = [result]  except web.HTTPError, e:    result = [e.data]  result = web.safestr(iter(result))  status, headers = web.ctx.status, web.ctx.headers  start_resp(status, headers)  def cleanup():    self._cleanup()    yield '' # force this function to be a generator  return itertools.chain(result, cleanup())for m in middleware:   wsgi = m(wsgi)return wsgi

下面來仔細分析一下這個函數:

  self._cleanup()  self.load(env)

self._cleanup()內部調用utils.ThreadedDict.clear_all(),清除所有的thread local資料,避免記憶體泄露(因為web.py架構的很多資料都會儲存在thread local變數中)。

self.load(env)使用env中的參數初始化web.ctx變數,這些變數涵蓋了當前請求的資訊,我們在應用中有可能會使用到,比如web.ctx.fullpath。

  try:    # allow uppercase methods only    if web.ctx.method.upper() != web.ctx.method:      raise web.nomethod()    result = self.handle_with_processors()    if is_generator(result):      result = peep(result)    else:      result = [result]  except web.HTTPError, e:    result = [e.data]

這一段主要是調用self.handle_with_processors(),這個函數會對請求的URL進行路由,找到合適的類或子應用來處理該請求,也會調用添加的處理器來做一些其他工作(關於處理器的部分,後面專門講)。對於處理的返回結果,可能有三種方式:

  1. 返回一個可迭代對象,則進行安全迭代處理。
  2. 返回其他值,則建立一個列表對象來存放。
  3. 如果拋出了一個HTTPError異常(比如我們使用raise web.OK("hello, world")這種方式來返回結果時),則將異常中的資料e.data封裝成一個列表。

-

  result = web.safestr(iter(result))  status, headers = web.ctx.status, web.ctx.headers  start_resp(status, headers)  def cleanup():    self._cleanup()    yield '' # force this function to be a generator  return itertools.chain(result, cleanup())

接下來的這段代碼,會對前面返回的列表result進行字串化處理,得到HTTP Response的body部分。然後根據WSGI的規範作如下兩個事情:

  1. 調用start_resp函數。
  2. 將result結果轉換成一個迭代器。

現在你可以看到,之前我們提到的application = app.wsgifunc()就是將wsgi函數賦值給application變數,這樣應用伺服器就可以採用WSGI標準和我們的應用對接了。
處理HTTP請求

前面分析的代碼已經說明了web.py架構如何?WSGI相容介面的,即我們已經知道了HTTP請求到達架構以及從架構返回給應用伺服器的流程。那麼架構內部是如何調用我們的應用代碼來實現一個請求的處理的呢?這個就需要詳細分析剛才忽略掉的處理器的添加和調用過程。
loadhook和unloadhook裝飾器

這兩個函數是真實處理器的函數的裝飾器函數(雖然他的使用不是採用裝飾器的@操作符),裝飾後得到的處理器分別對應請求處理之前(loadhook)和請求處理之後(unloadhook)。
loadhook

def loadhook(h):  def processor(handler):    h()    return handler()  return processor

這個函數返回一個函數processor,它會確保先調用你提供的處理器函數h,然後再調用後續的操作函數handler。
unloadhook

def unloadhook(h):  def processor(handler):    try:      result = handler()      is_generator = result and hasattr(result, 'next')    except:      # run the hook even when handler raises some exception      h()      raise    if is_generator:      return wrap(result)    else:      h()      return result  def wrap(result):    def next():      try:        return result.next()      except:        # call the hook at the and of iterator        h()        raise    result = iter(result)    while True:      yield next()  return processor

這個函數也返回一個processor,它會先調用參數傳遞進來的handler,然後再調用你提供的處理器函數。
handle_with_processors函數

def handle_with_processors(self):  def process(processors):    try:      if processors:        p, processors = processors[0], processors[1:]        return p(lambda: process(processors))      else:        return self.handle()    except web.HTTPError:      raise    except (KeyboardInterrupt, SystemExit):      raise    except:      print >> web.debug, traceback.format_exc()      raise self.internalerror()  # processors must be applied in the resvere order. (??)  return process(self.processors)

這個函數挺複雜的,最核心的部分採用了遞迴實現(我感覺不遞迴應該也能實現同樣的功能)。為了說明清晰,採用執行個體說明。

前面有提到,初始化application執行個體的時候,會添加兩個處理器到self.processors:

  self.add_processor(loadhook(self._load))  self.add_processor(unloadhook(self._unload))

所以,現在的self.processors是下面這個樣子的:

self.processors = [loadhook(self._load), unloadhook(self._unload)]

# 為了方便後續說明,我們縮寫一下:

self.processors = [load_processor, unload_processor]

當架構開始執行handle_with_processors的時候,是逐個執行這些處理器的。我們還是來看代碼分解,首先簡化一下handle_with_processors函數:

    def handle_with_processors(self):  def process(processors):    try:      if processors: # 位置2        p, processors = processors[0], processors[1:]        return p(lambda: process(processors)) # 位置3      else:        return self.handle() # 位置4    except web.HTTPError:      raise    ...  # processors must be applied in the resvere order. (??)  return process(self.processors) # 位置1

  • 函數執行的起點是位置1,調用其內部定義函數process(processors)。
  • 如果位置2判斷處理器列表不為空白,則進入if內部。
  • 在位置3調用本次需要執行的處理器函數,參數為一個lambda函數,然後返回。
  • 如果位置2判斷處理器列表為空白,則執行self.handle(),該函數真正的調用我們的應用代碼(下面會講到)。

以上面的例子來說,目前有兩個處理器:

self.processors = [load_processor, unload_processor]

從位置1進入代碼後,在位置2會判斷還有處理器要執行,會走到位置3,此時要執行代碼是這樣的:

return load_processor(lambda: process([unload_processor]))

load_processor函數是一個經過loadhook裝飾的函數,因此其定義在執行時是這樣的:

def load_processor(lambda: process([unload_processor])):  self._load()  return process([unload_processor]) # 就是參數的lambda函數

會先執行self._load(),然後再繼續執行process函數,依舊會走到位置3,此時要執行的代碼是這樣的:

return unload_processor(lambda: process([]))

unload_processor函數是一個經過unloadhook裝飾的函數,因此其定義在執行時是這樣的:

def unload_processor(lambda: process([])):  try:    result = process([]) # 參數傳遞進來的lambda函數    is_generator = result and hasattr(result, 'next')  except:    # run the hook even when handler raises some exception    self._unload()    raise  if is_generator:    return wrap(result)  else:    self._unload()    return result

現在會先執行process([])函數,並且走到位置4(調用self.handle()的地方),從而得到應用的處理結果,然後再調用本處理器的處理函數self._unload()。

總結一下執行的順序:

  self._load()    self.handle()  self._unload()

如果還有更多的處理器,也是按照這種方法執行下去,對於loadhook裝飾的處理器,先添加的先執行,對於unloadhook裝飾的處理器,後添加的先執行。
handle函數

講了這麼多,才講到真正要調用我們寫的代碼的地方。在所有的load處理器執行完之後,就會執行self.handle()函數,其內部會調用我們寫的應用代碼。比如返回個hello, world之類的。self.handle的定義如下:

def handle(self):  fn, args = self._match(self.mapping, web.ctx.path)  return self._delegate(fn, self.fvars, args)

這個函數就很好理解了,第一行調用的self._match是進行路由功能,找到對應的類或者子應用,第二行的self._delegate就是調用這個類或者傳遞請求到子應用。
_match函數

_match函數的定義如下:

def _match(self, mapping, value):  for pat, what in mapping:    if isinstance(what, application): # 位置1      if value.startswith(pat):        f = lambda: self._delegate_sub_application(pat, what)        return f, None      else:        continue    elif isinstance(what, basestring): # 位置2      what, result = utils.re_subm('^' + pat + '$', what, value)    else: # 位置3      result = utils.re_compile('^' + pat + '$').match(value)    if result: # it's a match      return what, [x for x in result.groups()]  return None, None

該函數的參數中mapping就是self.mapping,是URL路由映射表;value則是web.ctx.path,是本次請求路徑。該函數遍曆self.mapping,根據映射關係中處理對象的類型來處理:

  1. 位置1,處理對象是一個application執行個體,也就是一個子應用,則返回一個匿名函數,該匿名函數會調用self._delegate_sub_application進行處理。
  2. 位置2,如果處理對象是一個字串,則調用utils.re_subm進行處理,這裡會把value(也就是web.ctx.path)中的和pat匹配的部分替換成what(也就是我們指定的一個URL模式的處理對象字串),然後返回替換後的結果以及匹配的項(是一個re.MatchObject執行個體)。
  3. 位置3,如果是其他情況,比如直接指定一個類對象作為處理對象。

如果result非空,則返回處理對象和一個參數列表(這個參數列表就是傳遞給我們實現的GET等函數的參數)。
_delegate函數

從_match函數返回的結果會作為參數傳遞給_delegate函數:

fn, args = self._match(self.mapping, web.ctx.path)return self._delegate(fn, self.fvars, args)

其中:

  • fn:是要處理當前請求的對象,一般是一個類名。
  • args:是要傳遞給請求處理對象的參數。
  • self.fvars:是執行個體化application時的全域名稱空間,會用於尋找處理對象。

_delegate函數的實現如下:

def _delegate(self, f, fvars, args=[]):  def handle_class(cls):    meth = web.ctx.method    if meth == 'HEAD' and not hasattr(cls, meth):      meth = 'GET'    if not hasattr(cls, meth):      raise web.nomethod(cls)    tocall = getattr(cls(), meth)    return tocall(*args)  def is_class(o): return isinstance(o, (types.ClassType, type))  if f is None:    raise web.notfound()  elif isinstance(f, application):    return f.handle_with_processors()  elif is_class(f):    return handle_class(f)  elif isinstance(f, basestring):    if f.startswith('redirect '):      url = f.split(' ', 1)[1]      if web.ctx.method == "GET":        x = web.ctx.env.get('QUERY_STRING', '')        if x:          url += '?' + x      raise web.redirect(url)    elif '.' in f:      mod, cls = f.rsplit('.', 1)      mod = __import__(mod, None, None, [''])      cls = getattr(mod, cls)    else:      cls = fvars[f]    return handle_class(cls)  elif hasattr(f, '__call__'):    return f()  else:    return web.notfound()

這個函數主要是根據參數f的類型來做出不同的處理:

  • f為空白,則返回302 Not Found.
  • f是一個application執行個體,則調用子應用的handle_with_processors()進行處理。
  • f是一個類對象,則調用內建函式handle_class。
  • f是一個字串,則進行重新導向處理,或者擷取要處理請求的類名後,調用handle_class進行處理(我們寫的代碼一般是在這個分支下被調用的)。
  • f是一個可調用對象,直接調用。
  • 其他情況返回302 Not Found.
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.