tornado的Application的一些事儿

from tornado.httpserver import HTTPServer
from tornado.routing import RuleRouter, Rule, PathMatches, Router, HostMatches
from tornado.web import RequestHandler, Application, StaticFileHandler
from tornado.ioloop import IOLoop
import uimodules


class Http404(RequestHandler):
    def get(self):
        self.render(404.html)


class Handler1(RequestHandler):

    def initialize(self, k1):
        self.k1 = k1

    def get(self):
        print(self.reverse_url(index))
        self.write(1)


settins = {
    debug: False,  # 调试模式和自动重载
    default_handler_class: Http404,  # 如果没有匹配项就处理该类(比如自定义404页面)
    compress_response: True,
    ui_modules: uimodules,  # 设置UI模块,
    # ‘ui_methods‘: ‘uifunc‘,  # 设置UI方法,
    cookie_secret: 12312312,  # 用于设置cookie的secret
    login_url: /login,  # @authenticated如果用户未登陆,默认跳转到此url
    xsrf_cookies: True,  # 启用跨站点请求伪造保护。  {% module xsrf_form_html() %}

    # 模板相关
    autoescape: "xhtml_escape",  # 控制模板的自动转义,默认为"xhtml_escape"
    template_path: ./template,  # 模板的目录

    # 静态相关
    static_path: ./static,
    static_url_prefix: "/static/",  # 静态文件前缀 默认为"/static/"
}

application = Application([
    # (‘Matcher‘,‘处理规则‘,‘intiallizer的参数‘,‘反向解析用‘)
    (r"/handler", Handler1, {k1: v1}, index),
    (r"/static/(.*)", StaticFileHandler, {"path": "/var/www"}),
], **settins)

if __name__ == __main__:
    server = HTTPServer(application)
    server.listen(8888)
    IOLoop.current().start()

APP

tornado的Application的一些事儿

上一篇:P1046 陶陶摘苹果 Python实现


下一篇:CSAPP笔记(第十章 系统级I/O)