tornado.web包含web框架的大部分主要功能,Application是其中一个重要的类

Application类的作用是实现 URI 转发,将 Application 的实例传递给 httpserver ,当监听到请求时,把服务器传回来的请求进行转发,通过调用 __call__ ,处理请求。

Application源码:

class Application(httputil.HTTPServerConnectionDelegate):"""A collection of request handlers that make up a web application.Instances of this class are callable and can be passed directly toHTTPServer to serve the application::application = web.Application([(r"/", MainPageHandler),])http_server = httpserver.HTTPServer(application)http_server.listen(8080)ioloop.IOLoop.instance().start()The constructor for this class takes in a list of `URLSpec` objectsor (regexp, request_class) tuples. When we receive requests, weiterate over the list in order and instantiate an instance of thefirst request class whose regexp matches the request path.The request class can be specified as either a class object or a(fully-qualified) name.Each tuple can contain additional elements, which correspond to thearguments to the `URLSpec` constructor.  (Prior to Tornado 3.2, thisonly tuples of two or three elements were allowed).A dictionary may be passed as the third element of the tuple,which will be used as keyword arguments to the handler'sconstructor and `~RequestHandler.initialize` method.  This patternis used for the `StaticFileHandler` in this example (note that a`StaticFileHandler` can be installed automatically with thestatic_path setting described below)::application = web.Application([(r"/static/(.*)", web.StaticFileHandler, {"path": "/var/www"}),])We support virtual hosts with the `add_handlers` method, which takes ina host regular expression as the first argument::application.add_handlers(r"www\.myhost\.com", [(r"/article/([0-9]+)", ArticleHandler),])You can serve static files by sending the ``static_path`` settingas a keyword argument. We will serve those files from the``/static/`` URI (this is configurable with the``static_url_prefix`` setting), and we will serve ``/favicon.ico``and ``/robots.txt`` from the same directory.  A custom subclass of`StaticFileHandler` can be specified with the``static_handler_class`` setting."""def __init__(self, handlers=None, default_host="", transforms=None,**settings):if transforms is None:self.transforms = []if settings.get("compress_response") or settings.get("gzip"):self.transforms.append(GZipContentEncoding)else:self.transforms = transformsself.handlers = [] self.named_handlers = {}self.default_host = default_hostself.settings = settingsself.ui_modules = {'linkify': _linkify,'xsrf_form_html': _xsrf_form_html,'Template': TemplateModule,}self.ui_methods = {}self._load_ui_modules(settings.get("ui_modules", {}))self._load_ui_methods(settings.get("ui_methods", {}))if self.settings.get("static_path"):path = self.settings["static_path"]handlers = list(handlers or [])static_url_prefix = settings.get("static_url_prefix","/static/")static_handler_class = settings.get("static_handler_class",StaticFileHandler)static_handler_args = settings.get("static_handler_args", {})static_handler_args['path'] = pathfor pattern in [re.escape(static_url_prefix) + r"(.*)",r"/(favicon\.ico)", r"/(robots\.txt)"]:handlers.insert(0, (pattern, static_handler_class,static_handler_args))if handlers:self.add_handlers(".*$", handlers)if self.settings.get('debug'): # 如果debug = True, 开启autoreload 和 serve_traceback功能(出错显示错误信息), 关闭compiled_template_cache和static_hash_cache<span style="font-family: Arial, Helvetica, sans-serif;">功能</span>
self.settings.setdefault('autoreload', True)self.settings.setdefault('compiled_template_cache', False)self.settings.setdefault('static_hash_cache', False)self.settings.setdefault('serve_traceback', True)# Automatically reload modified modulesif self.settings.get('autoreload'): # 服务能够自动reload新的代码from tornado import autoreloadautoreload.start()def listen(self, port, address="", **kwargs):"""Starts an HTTP server for this application on the given port.This is a convenience alias for creating an `.HTTPServer`object and calling its listen method.  Keyword arguments notsupported by `HTTPServer.listen <.TCPServer.listen>` are passed to the`.HTTPServer` constructor.  For advanced uses(e.g. multi-process mode), do not use this method; create an`.HTTPServer` and call its`.TCPServer.bind`/`.TCPServer.start` methods directly.Note that after calling this method you still need to call``IOLoop.instance().start()`` to start the server.接受端口,地址,其它参数建立http服务器并监听该端口"""# import is here rather than top level because HTTPServer# is not importable on appenginefrom tornado.httpserver import HTTPServerserver = HTTPServer(self, **kwargs)server.listen(port, address)def add_handlers(self, host_pattern, host_handlers):"""Appends the given handlers to our handler list.Host patterns are processed sequentially in the order they wereadded. All matching patterns will be considered."""if not host_pattern.endswith("$"):host_pattern += "$"handlers = []# The handlers with the wildcard host_pattern are a special# case - they're added in the constructor but should have lower# precedence than the more-precise handlers added later.# If a wildcard handler group exists, it should always be last# in the list, so insert new groups just before it.if self.handlers and self.handlers[-1][0].pattern == '.*$':self.handlers.insert(-1, (re.compile(host_pattern), handlers))else:self.handlers.append((re.compile(host_pattern), handlers))for spec in host_handlers:if isinstance(spec, (tuple, list)):assert len(spec) in (2, 3, 4)spec = URLSpec(*spec)handlers.append(spec)if spec.name:if spec.name in self.named_handlers:app_log.warning("Multiple handlers named %s; replacing previous value",spec.name)self.named_handlers[spec.name] = specdef add_transform(self, transform_class):self.transforms.append(transform_class)def _get_host_handlers(self, request):host = request.host.lower().split(':')[0]matches = []for pattern, handlers in self.handlers:if pattern.match(host):matches.extend(handlers)# Look for default host if not behind load balancer (for debugging)if not matches and "X-Real-Ip" not in request.headers:for pattern, handlers in self.handlers:if pattern.match(self.default_host):matches.extend(handlers)return matches or Nonedef _load_ui_methods(self, methods):if isinstance(methods, types.ModuleType):self._load_ui_methods(dict((n, getattr(methods, n))for n in dir(methods)))elif isinstance(methods, list):for m in methods:self._load_ui_methods(m)else:for name, fn in methods.items():if not name.startswith("_") and hasattr(fn, "__call__") \and name[0].lower() == name[0]:self.ui_methods[name] = fndef _load_ui_modules(self, modules):if isinstance(modules, types.ModuleType):self._load_ui_modules(dict((n, getattr(modules, n))for n in dir(modules)))elif isinstance(modules, list):for m in modules:self._load_ui_modules(m)else:assert isinstance(modules, dict)for name, cls in modules.items():try:if issubclass(cls, UIModule):self.ui_modules[name] = clsexcept TypeError:passdef start_request(self, connection):# Modern HTTPServer interfacereturn _RequestDispatcher(self, connection)def __call__(self, request):# Legacy HTTPServer interfacedispatcher = _RequestDispatcher(self, None)dispatcher.set_request(request)return dispatcher.execute()def reverse_url(self, name, *args):"""Returns a URL path for handler named ``name``The handler must be added to the application as a named `URLSpec`.Args will be substituted for capturing groups in the `URLSpec` regex.They will be converted to strings if necessary, encoded as utf8,and url-escaped.根据name返回匹配的url路径"""if name in self.named_handlers:return self.named_handlers[name].reverse(*args)raise KeyError("%s not found in named urls" % name)def log_request(self, handler):"""Writes a completed HTTP request to the logs.By default writes to the python root logger.  To changethis behavior either subclass Application and override this method,or pass a function in the application settings dictionary as``log_function``.把HTTP请求写进日志http状态码小于400的为正常大于等于400小于500<span style="font-family: Arial, Helvetica, sans-serif;">为</span><span style="font-family: Arial, Helvetica, sans-serif;">请求错误</span>
大于500是服务器错误"""if "log_function" in self.settings:self.settings["log_function"](handler)returnif handler.get_status() < 400:log_method = access_log.infoelif handler.get_status() < 500:log_method = access_log.warningelse:log_method = access_log.errorrequest_time = 1000.0 * handler.request.request_time()log_method("%d %s %.2fms", handler.get_status(),handler._request_summary(), request_time)

构造函数

__init__(self,handlers=None,default_host="",transforms=None,**settings)

它接受handlers (包含匹配规则和requesthandler的元组),default_host(默认主机),transforms(输出做分块和压缩的转换)和setting(包含配置的字典)。

建立主机的路径路由规则

方法

listen(self, port, address="", **kwargs) 建立http服务器并监听该端口

add_handlers(self, host_pattern, host_handlers)向handler列表中添加handler。host_pattern依次按照它们的添加顺序进行处理,添加主机的路径路由规则

add_transform(self, transform_class)向self.transforms增加transform_class,对输出做分块和压缩的转换

_get_host_handlers(self, request)寻找属于这个request的handlers

_load_ui_methods(self, methods)在self.ui_methods中添加方法

_load_ui_modules(self, modules)在self.ui_modules中添加方法

reverse_url(self, name, *args)使用name返回匹配的url路径

__call__(self, request) 在服务器接受新连接时被调用 接受HttpRequest对象 根据 httprequest 调用execute 方法

start_request(self, connection)作用同__call__

log_request(self, handler)把HTTP请求写进日志。

debug=True 时代码动态自动编译的原理

Application 对象实例化时,给出“debug=True”参数的话,开启autoreload 和 serve_traceback功能(出错显示错误信息) , 关闭compiled_template_cache和static_hash_cache功能

autoreload:改变源代码的时候,服务器进程将能够自动reload新的代码并重启。

serve_traceback:如果开启,在出错的时候就不是返回默认的错误页面,取而代之的是显示python的traceback,利用这个我们可以看到在什么地方出现了错误,可以进行方便的调试。

compiled_template_cache:编译模板缓存,如果关闭,在刷新时服务器的模板都会重新加载。

static_hash_cache:静态哈希缓存,如果关闭,在刷新时有static_url()的地方都会重新载入需要读取的static下的文件,而不是用已经缓存了的文件。

tornado源码分析-Application相关推荐

  1. tornado源码分析

    tornado源码分析 本源码为tornado1.0版本 源码附带例子helloworld import tornado.httpserver import tornado.ioloop import ...

  2. Tornado源码分析 --- 静态文件处理模块

    每个web框架都会有对静态文件的处理支持,下面对于Tornado的静态文件的处理模块的源码进行分析,以加强自己对静态文件处理的理解. 先从Tornado的主要模块 web.py 入手,可以看到在App ...

  3. tornado源码分析系列一

    先来看一个简单的示例: #!/usr/bin/env python #coding:utf8import socketdef run():sock = socket.socket(socket.AF_ ...

  4. msdn windows server 按电源事件api_【tornado源码分析】I/O事件循环机制与多进程

    tornado是一个异步非阻塞的web框架,由Facebook开源,源代码用的python语言.之前也用tornado做过几个项目,实习的时候公司也是用的tornado来做工业物联网的后端.空余时间研 ...

  5. [原]tornado源码分析系列(三)[网络层 IOLoop类]

    引言:由于都是在工作当中抽出时间看源代码,所以更新速度比较慢,但是还是希望通过对好的源码的分析和探讨,大家相互学习,发现不好的地方共同讨论. 上次讲了IOLoop中的几个重要的方法,inistance ...

  6. tornado源码分析(四)之future、gen.coroutine

    future是什么 在事件驱动编程模型中,会有很多的事件循环,各事件循环在创建异步事件时可以同时创建一个future对象,并将创建的异步事件与该future对象存储在一起,并将所有传入的callbac ...

  7. 【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | Instrumentation 源码分析 )

    文章目录 一.Instrumentation 源码分析 二.Instrumentation 创建 Application 相关的部分源码 dex 解密时 , 需要将 代理 Application 替换 ...

  8. 【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | LoadedApk 源码分析 )

    文章目录 一.LoadedApk 源码分析 二.LoadedApk 源码 makeApplication 方法分析 dex 解密时 , 需要将 代理 Application 替换为 真实 Applic ...

  9. 【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | ActivityThread 源码分析 )

    文章目录 一.ActivityThread 源码分析 二.ActivityThread 部分代码示例 dex 解密时 , 需要将 代理 Application 替换为 真实 Application ; ...

最新文章

  1. 多模态数据+知识图谱,这次你的疑难杂症有解了!
  2. Nat. Mach. Intell. | 探索稀疏化学空间的化学语言模型新策略
  3. python之flask框架详解
  4. linux 查看目录挂载的ip,Linux挂载IPSAN和FCSAN操作,Linux挂载NFS文件系统
  5. arduino代码运行时间测试函数,代码性能运行时间测试方法
  6. Win10 Powershell ssh到WSL
  7. JAVA多线程之synchronized和volatile实例讲解
  8. oracle for aix 7,ORACLE 11.2.0.2 RAC for AIX 7.1
  9. 2022年计算机二级C语言程序设计模拟练习题及答案
  10. office韩文版本
  11. 网络丢包怎么办?这些解决办法
  12. 多个blockquote_换句话说:使用blockquote,cite和q元素
  13. 尚医通【预约挂号系统】总结
  14. 笑过后值得思考的一些编程语录----------搞笑却真实
  15. chrome点击下载链接没有反应
  16. CSS篇-dbMovies和dbBooks小网页
  17. UWP 记一次WTS 和 UCT翻车经历
  18. 将Android布局转成图片,并保存到本地(解决JPEG图片因透明度变黑问题)
  19. MFRC522模块测试
  20. 已知三角形三个顶点求内切圆和外接圆半径 以及面积 C语言模板

热门文章

  1. Git让你从入门到精通,看这一篇就够了!
  2. 接口压力测试:Siege压测安装、使用和说明
  3. win10系统升级没有声音?老司机教你怎么重新安装声卡驱动
  4. Banana Pi 消息
  5. 微信小程序03---开放能力的使用、发布和分享、组件化
  6. 最新双色球彩万宝缩水使用方法大全
  7. Lottie 免费动画、在线预览
  8. 什么是docker,它与虚拟机有什么区别?
  9. Apache Structured Streaming_JZZ158_MBY
  10. Zotero + connected papers论文顺藤摸瓜