Tornado 使用手册(一)---------- 简单的tornado配置

#Tornado 使用手册(一)---------- 简单的tornado配置

1. 简单的web.py

		import tornado.ioloop
		import tornado.web

		import os
		from tornado.options import options, define
		class 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, options
	define('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.ioloop
    import tornado.web
    
    import os
    from 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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值