可乐博客

Tornado技巧 如何做类nginx多host dns单端口解析

直接上代码

import asyncio
import tornado.web


class txtPathHandler(tornado.web.RequestHandler):
    def get(self, path):
        print('this', path)
        self.write("Hello, world")


class comPathHandler(tornado.web.RequestHandler):
    def get(self, path):
        print('this', path)
        # print(dir(self))
        print(self.request.method)
        print(self.request.uri)
        print(self.request.host)
        self.write("Hello, com")


application = tornado.web.Application([
    # (r"/", MainHandler),
    (r"/(.*)", txtPathHandler),
])

application.add_handlers(r"(.*)$", [
    (r"/(.*)", comPathHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    # tornado.ioloop.IOLoop.instance().start()
    loop = asyncio.get_event_loop()
    loop.run_forever()
Pythonic