(tornado源碼分析_004)HTTP伺服器處理解析出來的http資料

來源:互聯網
上載者:User

標籤:tar   處理   完整   size   soc   compress   調用   啟動方式   ESS   

tornado中HTTP伺服器是承上啟下的作用,它通過tornado.http1connection.HTTP1ServerConnection與tornado.http1connection.HTTP1Connection從socket中讀取並解析http訊息

然後調用application處理解析出來的http訊息,具體方法為:將application作為資料處理類傳給上述兩個讀取資料的類

具體代碼如下

#常見的torando啟動方式application = tornado.web.Application(Handlers)application.listen(8888) class Application(ReversibleRouter):    def listen(self):        #啟動時建立HTTPServer        from tornado.httpserver import HTTPServer        server = HTTPServer(self, **kwargs)        server.listen(port, address)        return serverclass HTTPServer(TCPServer,Configurable,httputil.HTTPServerConnectionDelegate):       def __init__(self, application):        #將application封裝成_CallableAdapter,因為application負責處理完整的request,        self.delegate = _CallableAdapter(application)class HTTP1Connection(httputil.HTTPConnection):    def read_response(self, delegate):        if self.params.decompress:            delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)        return self._read_message(delegate)    def _read_message(self, delegate):        #調用上層即HTTPServer處理解析出來的http訊息頭部,該處理也是非同步        delegate.headers_received(start_line, headers)        def _read_body(self, code, headers, delegate):        #body可能較大,時分區到達的        delegate.data_received(chunk)class _CallableAdapter(httputil.HTTPMessageDelegate):    def __init__(self, request_callback, request_conn):        self.connection = request_conn        self.request_callback = request_callback        self.request = None        self.delegate = None        self._chunks = []    def headers_received(self, start_line, headers):        #當代用該函數時,說明是一個新的請求,建立request        self.request = httputil.HTTPServerRequest(            connection=self.connection, start_line=start_line,            headers=headers)    def data_received(self, chunk):        #body是分區到達的,儲存,以便最後裝配        self._chunks.append(chunk)    def finish(self):        #組裝body        self.request.body = b‘‘.join(self._chunks)        self.request._parse_body()                #調用application(tornado的web架構)處理request        self.request_callback(self.request)    def on_connection_close(self):        self._chunks = None

 

(tornado源碼分析_004)HTTP伺服器處理解析出來的http資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.