Sanic

编程入门 行业动态 更新时间:2024-10-09 19:14:37

<a href=https://www.elefans.com/category/jswz/34/1684560.html style=Sanic"/>

Sanic

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

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

第一个蓝图

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

from sanic import Blueprint
from sanic.response import textbp = 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 bpapp = Sanic()
app.blueprint(bp)if __name__ == "__main__":app.run()

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

蓝图的使用

在上一篇博文中简单介绍了一下在路由中如何使用中间件与异常以及监听器等,这些东西在蓝图中同样可以使用:

  • 中间件:使用蓝图可以在全局范围内注册中间件
@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 textbp1 = 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,bp2app = 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为定义蓝图实例中的nameurl = 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))

更多推荐

Sanic

本文发布于:2024-03-11 18:13:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1729581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Sanic

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!