Tornadao—配置

  1. 命令行参数


    import tornado.web
    import tornado.ioloop
    import tornado.httpserver
    import tornado.options # 新导⼊的options模块
    tornado.options.define("port", default=8000, type=int, help="run
    server on the given port.") # 定义服务器监听端⼝选项
    tornado.options.define("itcast", default=[], type=str, multiple=True,
    help="itcast subjects.") # ⽆意义,演示多值情况
    class IndexHandler(tornado.web.RequestHandler):
     """主路由处理类"""
     def get(self):
     """对应http的get请求⽅式"""
     self.write("Hello Itcast!")
    if __name__ == "__main__":
     tornado.options.parse_command_line()
     app = tornado.web.Application([
     (r"/", IndexHandler),
     ])
     print(tornado.options.options.itcast)
    http_server = tornado.httpserver.HTTPServer(app)
     http_server.listen(tornado.options.options.port)
     tornado.ioloop.IOLoop.current().start()
    终端执行
    python opt.py --port=9000 --itcast=python,c++,java,php,ios
  2. 配置文件


    应⽤程序对象,也可以进⾏配置,⽐如模板⽂件路径,静态资源路径,是否是调试
    模式等
    if __name__ == '__main__':
     #创建⼀个应⽤对象
     settings = dict(
     template_path = 'templates',
     static_path='static',
     debug = True #调试模式
     )
     app = tornado.web.Application([(r'/',IndexHandler)], **settings)
     #绑定⼀个监听端⼝
     app.listen(8888)
     #启动web程序,开始监听端⼝的连接
     tornado.ioloop.IOLoop.current().start()
    单独写一个
    #settings.py
    settings = {
     'template_path': os.path.join(os.path.dirname(__file__),
    'templates'),
     'static_path': os.path.join(os.path.dirname(__file__),
    'statics'),
     'debug':True,
    }
    # 使用
    import tornado.web
    import config
    .......
    if __name__ = "__main__":
     app = tornado.web.Application([], **config.settings)
     .........
发布了209 篇原创文章 · 获赞 6 · 访问量 2916

猜你喜欢

转载自blog.csdn.net/piduocheng0577/article/details/105058927