1.Tornado
- Tornado:python编写的web服务器兼web应用框架
1.Tornado的优势
- 轻量级web框架
- 异步非阻塞IO处理方式
- 出色的抗负载能力
- 优异的处理性能,不依赖多进程/多线程,一定程度上解决C10K问题
- WSGI全栈替代产品,推荐同时使用其web框架和HTTP服务器
2.Tornado VS Django
- Django:重量级web框架,功能大而全,注重高效开发
- 内置管理后台
- 内置封装完善的ORM操作
- session功能
- 后台管理
- 缺陷:高耦合
- Tornado:轻量级web框架,功能少而精,注重性能优越
- HTTP服务器
- 异步编程
- WebSocket
- 缺陷:入门门槛较高
2.安装
输入命令:
pip install tornado
备注:
Tornado应该运行在类Unix平台,为了达到最佳的性能和扩展性,仅推荐Linux和BSD(充分利用Linux的epoll工具和BSD的kqueue达到高性能处理的目的)
3.使用
1.Tornado入门程序 - (一)
#-*- coding:utf-8 -*-
import tornado.web
import tornado.ioloop
#定义处理类型
class IndexHandler(tornado.web.RequestHandler):
#添加一个处理get请求方式的方法
def get(self):
#向响应中,添加数据
self.write('好看的皮囊千篇一律,有趣的灵魂万里挑一。')
if __name__ == '__main__':
#创建一个应用对象
app = tornado.web.Application([(r'/',IndexHandler)])
#绑定一个监听端口
app.listen(8888)
#启动web程序,开始监听端口的连接
tornado.ioloop.IOLoop.current().start()
1 .在pycharm中直接运行代码
2 .如果是在ubuntu,在命令窗口输入python 文件名.py
4.Tornado 代码解析
1.入门程序代码解析
-
tornado.web:tornado的基础web框架
-
RequestHandler
:封装对请求处理的所有信息和处理方法 -
get/post/..
:封装对应的请求方式 -
write()
:封装响应信息,写响应信息的一个方法
-
-
tornado.ioloop:核心io循环模块,封装linux的epoll和BSD的kqueue, tornado高性能处理的核心。
-
current()
返回当前线程的IOLoop实例对象 -
start()
启动IOLoop实力对象的IO循环,开启监听
-
2.httpserver底层处理
-
httpserver监听端口
tornado.httpserver.HTTPServer(app)
httpserver.listen(port)
-
httpserver实现多进程操作
tornado.httpserver.HTTPServer(app)
httpserver.bind(port)
httpserver.start(0/None/<0/num)
# -*- coding:utf-8 -*-
from tornado.web import Application,RequestHandler
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
class IndexHandler(RequestHandler):
def get(self):
self.write('给自己一点时间,理清所有的荒唐与期望。')
if __name__ == '__main__':
app = Application([(r'/',IndexHandler)])
http_server = HTTPServer(app)
#最原始的方式
http_server.bind(8888)
http_server.start(0)
#启动Ioloop轮循监听
IOLoop.current().start()
3.options配置
- 全局配置
tornado.options.define(
name, default, type, multiple, help
)
- 命令行参数转换
tornado.options.parse_command_line()
#-*- coding:utf-8 -*- from tornado.web import RequestHandler,Application from tornado.ioloop import IOLoop from tornado.httpserver import HTTPServer import tornado.options #定义变量 tornado.options.define('port',default=8000,type=int,help="this is the port >for application") class IndexHandler(RequestHandler): def get(self): self.write('我们既然改变不了规则,那就做到最好') if __name__ == '__main__': app = Application([(r'/',IndexHandler)]) tornado.options.parse_command_line() http_server = HTTPServer(app) http_server.bind(tornado.options.options.port) http_server.start(1) #启动IOLoop轮循监听 IOLoop.current().start()
- 配置文件
#即在当前py文件目录创建config文件,并在py代码中加入以下代码,
tornado.options.parse_config_file("./config")
- 配置模块:跟配置文件类似
4.application配置
- 程序调试之debug配置
#自动重启+取消缓存模板+取消缓存静态文件+提供追踪信息
tornado.web.Application([(..)], debug=True)
注:开发之初可以设置debug=True方便调试,开发完毕改为False.
- 路由信息初始化参数配置
tonado.web.Application([(r””, Handler, {k:v})])
def initialize(self, k)
#-*- coding:utf-8 -*- from tornado.web import RequestHandler,Application from tornado.ioloop import IOLoop from tornado.httpserver import HTTPServer from tornado.options import define,options define('port',default=8000,type=int) class IndexHandler(RequestHandler): def get(self): self.write('get-->先自沉稳,而后爱人') class ArticleHandler(RequestHandler): def initialize(self,title): print('-->initialize()') self.title = title def get(self): self.write('你正在查看文章:%s'% self.title) if __name__ == '__main__': options.parse_command_line() app = Application([(r'/',IndexHandler),(r'/article',ArticleHandler,{'title':'你>希望自己成为什么样的人,最终就会成为那样的人。'})],debug=True) http_server = HTTPServer(app) http_server.bind(options.port) http_server.start(1) IOLoop.current().start()
- 路由名称设置及反解析
#名称设置
tornado.web.Application([
url(r””, handler, {k,v}, name=“”)
])
#反解析操作
reverse_url(name)
# -*- coding:utf-8 -*- from tornado.web import Application, RequestHandler, url from tornado.ioloop import IOLoop from tornado.httpserver import HTTPServer from tornado.options import options,define define("port", default=8000, type=int) class IndexHandler(RequestHandler): def get(self): self.write("<a href='"+self.reverse_url("login")+"'>用户登录</a>") class RegistHandler(RequestHandler): def initialize(self, title): self.title = title def get(self): self.write("注册业务处理:" + str(self.title)) class LoginHandler(RequestHandler): def get(self): self.write("用户登录页面展示") def post(self): self.write("用户登录功能处理") if __name__ == "__main__": app = Application( [ (r"/", IndexHandler), (r"/regist", RegistHandler, {"title": "会员注册"}), url(r"/login", LoginHandler, name="login"), ] ) http_server = HTTPServer(app) http_server.listen(8000) IOLoop.current().start()
5.参数传递
- get方式传递参数
get_query_arguments(name,default=_ARG_DEFAULT,strip=True)
get_query_argument(name ,strip=True)
# -*- coding:utf-8 -*- from tornado.web import Application, RequestHandler from tornado.ioloop import IOLoop from tornado.httpserver import HTTPServer from tornado.options import options, define define("port", default=8000, type=int) class IndexHandler(RequestHandler): def get(self): # 获取get方式传递的参数 username = self.get_query_argument("username") usernames = self.get_query_arguments("username") print (username) print (usernames) if __name__ == "__main__": app = Application([(r"/", IndexHandler)]) app.listen(8000) IOLoop.current().start()
- post方式传递参数
get_body_arguments(name, default=_ARG_DEFAULT,strip=True)
get_body_argument(name ,strip=True)
- 混合方式
get_arguments(..)/get_argument(..)
# -*- coding:utf-8 -*- from tornado.web import Application, RequestHandler from tornado.ioloop import IOLoop from tornado.options import options, define define("port", default=8000, type=int) class IndexHandler(RequestHandler): def get(self): # 获取get方式的参数 user = self.get_argument("user") print("get方式获取参数:" + str(user)) def post(self): # 获取post方式的参数 user = self.get_argument("user") print("post方式获取参数:" + user.encode("utf-8")) if __name__ == "__main__": app = Application([(r"/", IndexHandler)]) app.listen(8888) IOLoop.current().start()
- 其他参数
通过request获取参数数据
method/host/uri/path/query/version/headers/body/remote_ip/files
6.响应头设置
- set_header(name, value)
- set_default_headers(self)
- add_header(name, value)
- clear_header(name)
# -*- coding:utf-8 -*-
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from tornado.options import options, define
define("port", default=8000, type=int)
class IndexHandler(RequestHandler):
def set_default_headers(self):
# 第二种响应头设置方式
print("---------> 响应头set_default_headers()执行")
self.set_header("Content-type", "application/json; charset=utf-8")
self.set_header("js", "zj")
def get(self):
# 第一种操作响应头的方式:
# self.set_header("Content-type", "application/json")
print("---------->get方法执行")
self.write("{'简书':'知几'}")
self.set_header("jianshu", "zhiji")
if __name__ == "__main__":
app = Application([(r"/", IndexHandler)])
app.listen(8000)
IOLoop.current().start()
7.cookie操作
- cookies
set/get_cookie(name, value)
set/get_secure_cookie(name, value)
clear_cookie(name)
clear_all_cookie()
# -*- coding:utf-8 -*-
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from tornado.options import options, define
define("port", default=8000, type=int)
class IndexHandler(RequestHandler):
def get(self):
self.write("hello jianshu.com")
self.set_cookie("loginuser", "admin")
print self.get_cookie("loginuser")
print self.cookies
if __name__ == "__main__":
app = Application([(r"/", IndexHandler)])
app.listen(8000)
IOLoop.current().start()
8.响应错误码 & 错误描述
- set_status(status_code, reason=None)
# -*- coding:utf-8 -*-
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from tornado.options import options, define
define("port", default=8000, type=int)
class IndexHandler(RequestHandler):
def get(self):
self.write("hello 简书")
# self.set_status(404) # 标准错误码 无描述使用默认描述
self.set_status(201, reason="zhiji is cool") # 自定义错误码,设置reason描述
# self.set_status(230) # 自定义错误码,无reason 报错
if __name__ == "__main__":
app = Application([(r"/", IndexHandler)])
app.listen(8000)
IOLoop.current().start()
- send_error(status_code, reason=None)
# -*- coding:utf-8 -*-
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from tornado.options import options, define
define("port", default=8000, type=int)
class IndexHandler(RequestHandler):
def get(self):
self.write("hello qikuedu.com")
self.send_error(500, reason="出错啦出错啦")
if __name__ == "__main__":
app = Application([(r"/", IndexHandler)])
app.listen(8000)
IOLoop.current().start()
- write_error(self, status_code, **kw)
# -*- coding:utf-8 -*-
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
from tornado.options import options, define
define("port", default=8000, type=int)
class IndexHandler(RequestHandler):
def get(self):
self.write("hello qikuedu.com")
self.send_error(404, msg="页面丢失", info="家里服务器搞对象去了")
def write_error(self, status_code, **kwargs):
self.write("<h1>出错啦,工程师MM正在赶来的途中...</h1>")
self.write("<p>错误信息:%s</p>" % kwargs["msg"])
self.write("<p>错误描述:%s</p>" % kwargs["info"])
if __name__ == "__main__":
app = Application([(r"/", IndexHandler)])
app.listen(8000)
IOLoop.current().start()