import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # 提供了多种选择 python xxx.py --port=xxxx from tornado.options import define,options define('port',default=8000,help='run port',type=int) # windows通过Ctrl+鼠标左键 define('version',default=0.1,help='version',type=str) class MainHandler(tornado.web.RequestHandler): def get(self): self.render("ces.html") # 写入页面 class AHandler(tornado.web.RequestHandler): def get(self): self.set_header("a",1) # 设置响应头 self.add_header("b",2) # 添加 可重复名字 self.clear_header("a") # 撤销(删除) class SendHandler(tornado.web.RequestHandler): def get(self): self.write("zzz") # self.flush() 有flush 仅对send_error不起作用 self.write("不怕flush") # self.finish() # 彻底结束 self.send_error(404) # 主动发出错误码 def write_error(self, status_code, **kwargs): # 会覆盖前面的缓冲区内的内容 self.write("重写错误信息,覆盖前面的缓冲区内的内容") self.render("error.html") class NotFoundHandler(tornado.web.RequestHandler): def write_error(self, status_code, **kwargs): self.write("路由没找到") class StatusHandler(tornado.web.RequestHandler): def get(self): self.set_status(404, "error") # 修改状态码 class IndexHandler(tornado.web.RequestHandler): def set_default_headers(self): print("first--设置headers") def initialize(self): print("second--初始化") def prepare(self): print("third--准备工作") def get(self): print("fourth--处理get请求") def post(self): print('fourth--处理post请求') def write_error(self, status_code, **kwargs): print("fifth--处理错误") def on_finish(self): print("sixth--c处理结束,释放资源--") class GetHandler(tornado.web.RequestHandler): def get(self): # 处理get请求 name = self.get_argument("name","no") self.write(name) name = self.get_arguments("name") print(name) # list self.write(str(name)) def post(self, *args, **kwargs): # 处理post请求 name = self.get_argument("name","no") passwd = self.get_argument("password","no") self.write("name: %s <br> password:%s" %(name,passwd)) name1 = self.get_body_argument("name","none") print(name1) class UserHandler(tornado.web.RequestHandler): def get(self,name,age): self.write("name:%s<br>age:%s" %(name,age)) application = tornado.web.Application( handlers=[ # 列表按顺序匹配 (r"/", MainHandler), (r"/a", AHandler), (r"/send", SendHandler), (r"/sta", StatusHandler), (r"/index",IndexHandler), (r"/get",GetHandler), (r"/user/(.+)/([0-9]+)",UserHandler), (r"/user1/(?P<age>[0-9]+)/(?P<name>.+)",UserHandler), # ?P<>取名 (r"/.*", NotFoundHandler) ], template_path='templates', # 表明页面html的路径 debug=True # 上传代码后服务器自动重启 ) if __name__ == '__main__': tornado.options.parse_command_line() # 通过sys.arg获取命令行输入参数(python xxx.py --port=xxx) print(options.port) print(options.version) http_server = tornado.httpserver.HTTPServer(application) # 非阻塞 application.listen(options.port) tornado.ioloop.IOLoop.instance().start() # 启动io循环
# 知识点
# 请求响应
# 浏览器发起请求(url)--> 服务器开始处理请求,处理结束 --> 返回处理结果到页面-->页面展示
# 增删改header
# self.set_header
# add_header
# clear_header
# 改写错误信息
# 重写write_error方法 def write_error
# 找不到路由(写上匹配任何路由 | r"/.*",NotFoundHandler | ,再改写路由错误信息)
# 修改状态码 status
# self.set_status(404, error)
# tornado接受请求处理过程 (了解)
# first--设置headers
# second--初始化
# third--准备工作
# fourth--处理get请求
# sixth--c处理结束,释放资源--