(tornado源码分析_004)HTTP服务器处理解析出来的http数据
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了(tornado源码分析_004)HTTP服务器处理解析出来的http数据,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1950字,纯文字阅读大概需要3分钟。
内容图文
![(tornado源码分析_004)HTTP服务器处理解析出来的http数据](/upload/InfoBanner/zyjiaocheng/1247/73d29112ed8f4720b2b21a809227002d.jpg)
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): #启动时创建HTTPServerfrom tornado.httpserver import HTTPServer server = HTTPServer(self, **kwargs) server.listen(port, address) return server class 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
原文:https://www.cnblogs.com/madun/p/9531758.html
内容总结
以上是互联网集市为您收集整理的(tornado源码分析_004)HTTP服务器处理解析出来的http数据全部内容,希望文章能够帮你解决(tornado源码分析_004)HTTP服务器处理解析出来的http数据所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。