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

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值