码迷,mamicode.com
首页 > 其他好文 > 详细

tornado的路由分发

时间:2019-11-30 21:09:37      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:ret   omr   头部   providing   https   ini   handlers   request   type   

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的路由分发

标签:ret   omr   头部   providing   https   ini   handlers   request   type   

原文地址:https://www.cnblogs.com/liuer-mihou/p/11963639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!