set_header    设置响应头

clear_header 清除响应头

add_header   增加响应头

self.flush  self.finish  中断

set_status     设置状态码

工作流程

  lesso3.py

 # -*- coding:utf-8 -*-

 import tornado.web
import tornado.httpserver
import tornado.options
import tornado.ioloop
from tornado.options import define,options define('port', default=8080, help='run port', type=int)
define('version', default='0.0.1', help='version 0.0.1', type=str) class SetHandler(tornado.web.RequestHandler): def get(self):
self.write('set_header')
self.set_header('aaa',111) # 设置响应头
self.set_header('ccc',222)
self.set_header('ccc',333) # 重复设置会覆盖前面的
self.clear_header('ccc') # 清除响应头
self.add_header('ccc',333) # 增加响应头 class AddHandler(tornado.web.RequestHandler): def get(self):
self.write('add_header')
self.add_header('abc',444) # 增加响应头 class SendHandler(tornado.web.RequestHandler): def get(self):
self.write('send_error <br>')
# self.flush() # 如果加上这个flush会小小的中断一下 后面的send_error 不会执行 write 会执行
# self.finish() # 如果加上这个finish会彻底中断,后面的任何指令都不会执行
self.write('我被执行了')
self.send_error(404) class NotFoundHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs):
self.send_error(404) def write_error(self, status_code, **kwargs):
self.render('error.html') class StatusHandler(tornado.web.RequestHandler): def get(self, *args, **kwargs): # 重写状态码
self.write('set_status')
self.set_status(200,'every good !') '''工作流程'''
class IndexHandler(tornado.web.RequestHandler): def set_default_headers(self):
print('-----set_default_headers:设置headers----') def initialize(self):
print('----initialize: 初始化----') def prepare(self):
print('----prepare:准备工作----') def get(self, *args, **kwargs):
self.write('----get 处理请求----') def post(self, *args, **kwargs):
self.write('----post 处理请求----') def write_error(self, status_code, **kwargs):
self.render('error.html') def on_finish(self):
print('----on_finish: 处理结束 释放资源----') application = tornado.web.Application(
handlers = [
(r"/",SetHandler),
(r"/add",AddHandler),
(r"/send",SendHandler),
(r"/status",StatusHandler),
(r"/index",IndexHandler),
(r"/(.*)",NotFoundHandler),
],
template_path = './error',
debug = True,
) if __name__ == '__main__':
print(options.port)
print(options.version)
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
05-11 13:55