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下面, 将整个项目划分成各个小模块来管理

相关文章:

  • 2022-02-13
  • 2021-06-30
  • 2021-08-31
  • 2022-12-23
  • 2021-09-18
  • 2021-07-24
  • 2021-05-17
  • 2021-08-13
猜你喜欢
  • 2021-12-18
  • 2022-12-23
  • 2021-11-18
  • 2021-05-20
  • 2022-01-18
  • 2022-12-23
  • 2021-08-21
相关资源
相似解决方案