日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tornado学习笔记18 _RequestDispatcher 请求分发器

發布時間:2025/3/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tornado学习笔记18 _RequestDispatcher 请求分发器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

根據Application的配置,主要負責將客戶端的請求分發到具體的RequestHandler。這個類實現了HTTPMessageDelegate接口。

18.1 構造函數

定義:

def __init__(self, application, connection):

參數:

application:Application對象。

connection:請求連接,為HTTP1Connection實例。

實現分析:

就是對這個類的屬性進行初始化賦值。比如chunks,handler_class(RequestHandler處理類)、handler_kwargs(處理類參數)、path_args、path_kwargs等。

18.2 HTTPMessageDelegate接口實現

1.2.1 header_received

定義

def headers_received(self, start_line, headers):

實現分析:

實例化HTTPServerRequest對象,調用set_request方法,set_request方法中去查找匹配的請求處理類(RequestHandler)。如果請求處理類實現了_stream_request_body方法,則直接調用execute方法。

1.2.2 data_received

定義

def data_received(self, data):

實現過程:

如果RequestHandler實現了_stream_request_body方法,則調用handler的data_received方法,如果沒實現,則將數據塊添加至chunks中。

1.2.3 finish

當消息數據塊接收完畢后,調用此方法。此方法的實現就是將數據塊連接起來,然后調用HTTPServerReuqest的_parse_body方法,實現對body體消息的解析。然后調用excute方法。

18.3 其他方法

1.3.1 _find_handler

這個方法很重要,也是核心方法之一。實現Application的配置屬性以及請求路徑的匹配,找到匹配的RequestHandler。

實現過程:

(1) 調用Application的_get_host_handlers方法,獲得匹配的hanlders集合;

(2) 如果沒有匹配到合適的handlers,將handler_class設置成RedirectHandler,并設置handler_kwargs,然后返回。

(3) 如果匹配到了合適的handlers,循環handlers中的每一元素URLSpec,判斷其中的路徑正則表達式是否與請求路徑相匹配。

(4) 如果匹配合適的RequestHandler,設置handler_class以及handler_kwargs,而后設置path_kwargs

(5) 沒有沒有匹配到RequestHandler,判斷Application是否設置了default_handler_class選項,并設置handler_class為其值。如果沒有設置default_handler_class選項,則將handler_class屬性設置成 ErrorHandler,狀態碼設置成404錯誤,也就是not found錯誤。

18.3.2 execute

這個方法很核心,也很重要。當請求信息處理完畢后(調用finish方法后),會執行execute方法。方法如下:

def execute(self): # If template cache is disabled (usually in the debug mode),# re-compile templates and reload static files on every# request so you don't need to restart to see changesif not self.application.settings.get("compiled_template_cache", True):with RequestHandler._template_loader_lock:for loader in RequestHandler._template_loaders.values():loader.reset()if not self.application.settings.get('static_hash_cache', True):StaticFileHandler.reset()self.handler = self.handler_class(self.application, self.request,**self.handler_kwargs)transforms = [t(self.request) for t in self.application.transforms]if self.stream_request_body:self.handler._prepared_future = Future()# Note that if an exception escapes handler._execute it will be# trapped in the Future it returns (which we are ignoring here,# leaving it to be logged when the Future is GC'd).# However, that shouldn't happen because _execute has a blanket# except handler, and we cannot easily access the IOLoop here to# call add_future (because of the requirement to remain compatible# with WSGI)f = self.handler._execute(transforms, *self.path_args,**self.path_kwargs)# If we are streaming the request body, then execute() is finished# when the handler has prepared to receive the body. If not,# it doesn't matter when execute() finishes (so we return None)return self.handler._prepared_future

實現過程描述如下:

(1) 判斷application是否對complied_template_cache是否設置成True.如果沒有,則將模板加載器重置。相當于不對編譯后模板緩存的話,就重置模板加載器。

(2) 判斷applacation是否對static_hash_cache是否設置成True. 如果沒有,則調用StaticFileHandler的reset方法。相當于不對網站的靜態文件進行緩存的話,就調用重置的方法。

(3) 根據_find_handler方法設置的handler_class屬性初始化自定義的RequestHanlder

(4) 調用requestHandler的_exucute方法,就是調用相應的方法,要門是get方法,要么是post,要么是其他支持的http方法,具體實現詳情請查看RequestHandler類。

轉載于:https://www.cnblogs.com/liaofeifight/p/5099440.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的tornado学习笔记18 _RequestDispatcher 请求分发器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。