tornado的路由分发

Posted liuer-mihou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tornado的路由分发相关的知识,希望对你有一定的参考价值。

1

from tornado.httpserver import HTTPServer
from tornado.httputil import HTTPServerConnectionDelegate, HTTPMessageDelegate, ResponseStartLine, HTTPHeaders
from tornado.routing import RuleRouter, Rule, PathMatches
from tornado.web import RequestHandler, Application
from tornado.ioloop import IOLoop

‘‘‘
RuleRouter([Rule的实例1,Rule的实例2])
‘‘‘

‘‘‘
第一种
Rule(Matcher,HTTPServerConnectionDelegate)
‘‘‘


class MessageDelegate(HTTPMessageDelegate):
    def __init__(self, connection):
        self.connection = connection

    def finish(self):
        self.connection.write_headers(
            ResponseStartLine("HTTP/1.1", 200, "OK"),  # HTTP响应的状态行
            HTTPHeaders({"Content-Length": "2", "k": V}),  # Response Headers 响应头部
            b"OK")  # 响应body
        self.connection.finish()


class ConnectionDelegate(HTTPServerConnectionDelegate):
    def start_request(self, server_conn, request_conn):
        print(server_conn, request_conn)
        return MessageDelegate(request_conn)


router = RuleRouter([
    Rule(PathMatches("/handler"), ConnectionDelegate()),
    # ... more rules
])

if __name__ == __main__:
    server = HTTPServer(router)
    server.listen(8888)
    IOLoop.current().start()
from tornado.httpserver import HTTPServer
from tornado.httputil import HTTPServerConnectionDelegate, HTTPMessageDelegate, ResponseStartLine, HTTPHeaders,     HTTPServerRequest
from tornado.routing import RuleRouter, Rule, PathMatches, Router
from tornado.web import RequestHandler, Application
from tornado.ioloop import IOLoop
import tornado.template

‘‘‘
RuleRouter([Rule的实例1,Rule的实例2])
‘‘‘

‘‘‘
第二种
Rule(Matcher,Router实例)
Router实例本质还是提供一个 HTTPMessageDelegate去处理
‘‘‘


class MessageDelegate(HTTPMessageDelegate):
    def __init__(self, connection):
        self.connection = connection

    def finish(self):
        self.connection.write_headers(
            ResponseStartLine("HTTP/1.1", 200, "OK"),
            HTTPHeaders({"Content-Length": "2"}),
            b"OK")
        self.connection.finish()


class CustomRouter(Router):
    def find_handler(self, request, **kwargs):
        # some routing logic providing a suitable HTTPMessageDelegate instance
        print(type(request))  # HTTPServerRequest
        print(request.connection)
        return MessageDelegate(request.connection)


router = RuleRouter([
    Rule(PathMatches("/router.*"), CustomRouter())
])

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

路由分发APP

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


class Handler1(RequestHandler):
    def get(self):
        self.write(1)


class Handler2(RequestHandler):
    def get(self):
        self.write(2)


app1 = Application([
    (r"/app1/handler", Handler1),
    # other handlers ...
])

app2 = Application([
    (r"/app2/handler", Handler2),
    # other handlers ...
])

router = RuleRouter([
    Rule(PathMatches("/app1.*"), app1),
    Rule(PathMatches("/app2.*"), app2)
])

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

 

根据域名匹配

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


class Handler1(RequestHandler):
    def get(self):
        self.write(1)


router = RuleRouter([
    Rule(HostMatches(monitor.com),
         RuleRouter([Rule(PathMatches(/app1.*), Application([(r/app1/handler1, Handler1)]))])),
    Rule(HostMatches(test.monitor.com),
         RuleRouter([Rule(PathMatches(/app1.*), Application([(r/app1/handler1, Handler1)]))])),
])

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

 

以上是关于tornado的路由分发的主要内容,如果未能解决你的问题,请参考以下文章

Tornado-基于正则的路由和动态分页

Android 事件分发事件分发源码分析 ( Activity 中各层级的事件传递 | Activity -> PhoneWindow -> DecorView -> ViewGroup )(代码片段

tornado 01 路由输入与输出

Django分发控制器urls--白话聊Django系列

VSCode自定义代码片段11——vue路由的配置

VSCode自定义代码片段11——vue路由的配置