Tornado 使用手册(一)---------- 简单的tornado配置
生活随笔
收集整理的這篇文章主要介紹了
Tornado 使用手册(一)---------- 简单的tornado配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
#Tornado 使用手冊(一)---------- 簡單的tornado配置
1. 簡單的web.py
import tornado.ioloopimport tornado.webimport osfrom tornado.options import options, defineclass MainHandler(tornado.web.RequestHandler):def get(self):self.write("hello,world")settings = {'debug': True,'gzip': True,'autoescape': None,'xsrf_cookies': False,'cookie_secret': 'xxxx'}application = tornado.web.Application([(r'/', MainHandler)])if __name__ == '__main__':application.listen(9002)tornado.ioloop.IOLoop.instance().start()##2. debug 參數配置
settings = {'debug': True, # 開發模式'gzip': True, # 支持gzip壓縮'autoescape': None,'xsrf_cookies': False,'cookie_secret': 'xxx'}application = tornado.web.Application([(r'/', MainHandler)], **settings)##3. 默認參數配置
# 在tornado.options.options配置變量名from tornado.options import define, optionsdefine('debug', default=True, help='enable debug mode')define('project_path', default=sys.path[0], help='deploy_path')define('port', default=8888, help='run on this port', type=int)# 從命令行中解析參數信息, 如 python web.py --port=9002, 這里的port解析tornado.options.parse_command_line()##4. 使用參數
使用options獲取剛設置的參數from tornado.options import options....application.listen(options.port).....settings = {'debug': options.debug,}##5. 完整代碼
#!/usr/bin/env python# -*- coding: utf-8 -*-# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:__author__ = 'okker.ma@gmail.com'import tornado.ioloopimport tornado.webimport osfrom tornado.options import options, define#在options中設置幾個變量define('debug', default=True, help='enable debug mode')define('port', default=9002, help='run on this port', type=int)# 解析命令行, 有了這行,還可以看到日志...tornado.options.parse_command_line()class MainHandler(tornado.web.RequestHandler):def get(self):self.write("hello,a world")settings = {'debug': options.debug,'gzip': True,'autoescape': None,'xsrf_cookies': False,'cookie_secret': 'xxxxxxx'}application = tornado.web.Application([(r'/', MainHandler)], **settings)if __name__ == '__main__':application.listen(options.port)tornado.ioloop.IOLoop.instance().start()運行:
python web.py --port=9002 --debug=True轉載于:https://my.oschina.net/jiemachina/blog/204875
總結
以上是生活随笔為你收集整理的Tornado 使用手册(一)---------- 简单的tornado配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 待字闺中:日志查询。(网络摘要)
- 下一篇: LAMP 搭建BBS论坛实战