您的位置:首页 > 其它

Sanic框架蓝图用法实例分析

2018-07-17 09:56 981 查看

本文实例讲述了Sanic框架蓝图用法。分享给大家供大家参考,具体如下:

蓝图是可以用于应用程序内子路由的对象。蓝图并未向应用程序内添加路由,而是定义了用于添加路由的类似方法,然后以灵活且可插入的方式向应用程序注册路由。

蓝图对于大型应用程序尤其有用,您的应用程序可以分解成为几个小组或责任区域。

第一个蓝图

假设你在bp/bp_1.py文件下,定义了以下这么一个非常简单的蓝图:

from sanic import Blueprint
from sanic.response import text
bp = Blueprint("first_bp")
@bp.route("/get_info")
async def get_info(request):
return text("it is ok!")

注册蓝图

定义了一个蓝图之后,必须在应用程序中注册:

from sanic import Sanic
from bp.bp_1 import bp
app = Sanic()
app.blueprint(bp)
if __name__ == "__main__":
app.run()

如此,并将蓝图添加到应用程序当中,并注册蓝图所定义的所有路由。此时我们就可以访问

/get_info
就可以获取到数据了

蓝图的使用

在前面一篇《Sanic框架异常处理与中间件操作》中简单介绍了一下在路由中如何使用中间件与异常以及监听器等,这些东西在蓝图中同样可以使用:

中间件:使用蓝图可以在全局范围内注册中间件

@bp.route("/get_info")
async def get_info(request):
return text("get_info")
@bp.middleware("request")
async def handle_md_request(request):
print("request middleware")
@bp.middleware("response")
async def handle_md_response(request,response):
print("response middleware")

异常:使用蓝图可以在全局范围内注册异常

from sanic.exceptions import NotFound
@bp.exception(NotFound)
async def handle_exception(request,exception):
return text("404 exception")

静态文件:静态文件可以在蓝图前缀下全局提供

bp.static("/home","/aaa/bbb/abc.html")

监听器:如果需要在服务器启动/关闭的时候,执行一些特殊的代码,则可以使用以下监听器,可用的监听器如下:

  • before_server_start:在服务器开始接收连接之前执行
  • after_server_start:在服务器开始接收连接之后执行
  • before_server_stop:在服务器停止接收连接之前执行
  • after_server_stop:在服务器停止接收连接之后执行
@bp.listener("before_server_start")
async def before_server_start(request,loop):
print("before server start")
@bp.listener("after_server_start")
async def after_server_start(request,loop):
print("after server start")
@bp.listener("before_server_stop")
async def before_server_stop(request,loop):
print("before server stop")
@bp.listener("after_server_stop")
async def after_server_stop(request,loop):
print("after server stop")

当服务器启动时,将会依次打印如下信息:

before server start
after server start

当服务器关闭时,将会依次打印如下信息:

before server stop
after server stop

API版本控制

与手机APP对接的接口开发中,API版本控制尤为重要,针对于低版本用户(尚未升级版本的用户)所用的仍是旧的接口数据,此时开发新功能时对此模块的数据需要进行修改,可是不能影响旧版本的用户,此时我们就需要对API版本进行控制。我们可以定义两个蓝图,一个指向

/v1/<route>
,另一个指向
/v2/<route>
。当蓝图初始化时,我们可以采用一个可选参数
url_prefix
,该参数将被置于蓝图定义的所有路由之上,这个特性可以来实现我们的API版本控制方案:

from sanic import Blueprint
from sanic.response import text
bp1 = Blueprint("bp1",url_prefix="/v1")
bp2 = Blueprint("bp2",url_prefix="/v2")
@bp1.route("/get_data")
async def get_v1_data(request):
return text("it is v1")
@bp2.route("/get_data")
async def get_v2_data(request):
return text("it is v2")

此时,我们已经定义好了两个蓝图来控制两个版本,我们需要在app中注册它们:

from sanic import Sanic
from app.bp.bp_info import bp1,bp2
app = Sanic()
app.blueprint(bp1)
app.blueprint(bp2)
if __name__ == "__main__":
app.run()

重定向

在之前的博文中,我们讲到可以使用

url_for
基于处理程序方法名称生成URL,然后使用
redirect
进行重定向,在蓝图中同样使用:

from sanic.response import text,redirect
@bp.route("/")
async def handle_root(request):
# bp为定义蓝图实例中的name
url = request.app.url_for("bp.get_info",name="laozhang")
return redirect(url)
@bp.route("/get_info/<name>")
async def get_info(request,name):
return text("name:{}".format(name))

更多关于Python相关内容可查看本站专题:《Python入门与进阶经典教程》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Sanic框架 蓝图