The HTTP server in tornado is a connecting link, It reads and resolves HTTP messages from the socket via tornado.http1connection.HTTP1ServerConnection and tornado.http1connection.HTTP1Connection
The application is then called to process the parsed HTTP message by passing the application as the data processing class to the two read data classes
The specific code is as follows
#Common Torando Start-up methodsApplication =tornado.web.Application (Handlers) Application.listen (8888) classApplication (reversiblerouter):defListen (self):#Create Httpserver at startup fromTornado.httpserverImportHttpserver Server= Httpserver (Self, * *Kwargs) Server.listen (port, address)returnServerclassHttpserver (Tcpserver,configurable,httputil. Httpserverconnectiondelegate):def __init__(self, application):#wrap the application into _callableadapter, as application is responsible for processing the complete request,Self.delegate =_callableadapter (application)classhttp1connection (Httputil. Httpconnection):defRead_response (Self, delegate):ifself.params.decompress:delegate=_gzipmessagedelegate (delegate, Self.params.chunk_size)returnSelf._read_message (delegate)def_read_message (Self, delegate):#call the upper layer that httpserver processing the parsed HTTP message header, the processing is also asynchronousdelegate.headers_received (start_line, headers)def_read_body (self, Code, headers, delegate):#body may be larger, when the Shard arrivesdelegate.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= [] defheaders_received (self, Start_line, headers):#contemporary with this function, the description is a new request to createSelf.request =Httputil. Httpserverrequest (Connection=self.connection, start_line=Start_line, headers=headers)defdata_received (self, chunk):#body is the Shard arrives, saved for final assemblyself._chunks.append (Chunk)defFinish (self):#Assemble bodySelf.request.body = b"'. Join (Self._chunks) self.request._parse_body ()#Call Application (Tornado Web framework) to process requestSelf.request_callback (self.request)defOn_connection_close (self): Self._chunks= None
(Tornado Source Analysis _004) HTTP Server processing the parsed HTTP data