zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    eclipse plugin development -menu
    Eclipse 插件开发 -- 深入理解菜单(Menu)功能及其扩展点( FROM IBM)
    eclipse core expression usage
    eclipse preference plugin development store and get
    eclipse default handler IHandler interface “the chosen operation is not enabled”
    sublime text 3-right click context menu
    SoapUI Pro Project Solution Collection-Custom project and setup
    SoapUI Pro Project Solution Collection-XML assert
    Cordova Ionic AngularJS
    centos
  • 原文地址:https://www.cnblogs.com/liuer-mihou/p/11963639.html
Copyright © 2011-2022 走看看