tornado實現不一樣app路由分發

tornado實現app路由分發

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


class Handler1(RequestHandler):
    def get(self):
        self.write('hello')


class Handler2(RequestHandler):
    def get(self):
        self.write('world')


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)
])

server = HTTPServer(router)

if __name__ == '__main__':
    server = HTTPServer(router)
    server.listen(8000)  # 改用服務器進行監聽
    ioloop.IOLoop.current().start()

這樣就能夠將不一樣的app整合到一個router下面, 將整個項目劃分紅各個小模塊來管理python

相關文章
相關標籤/搜索