Sanic框架蓝图用法实例分析

作者:喷跑的豆子 时间:2022-12-22 16:27:20 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/y472360651/article/details/80185896

标签:Sanic框架,蓝图
0
投稿

猜你喜欢

  • Python3中FuzzyWuzzy库实例用法

    2022-01-30 18:49:49
  • python中二维阵列的变换实例

    2021-06-28 07:54:06
  • 在函数间不能传递32个以上参数的疑难问题

    2008-12-31 13:31:00
  • Django实现jquery select2带搜索的下拉框

    2022-04-20 18:50:58
  • python如何删除字符串最后一个字符

    2022-06-07 14:14:04
  • 利用ASP发送和接收XML数据的处理方法

    2009-02-02 08:57:00
  • 在IE中使用高级CSS3选择器

    2010-01-22 15:20:00
  • 如何安装多版本python python2和python3共存以及pip共存

    2021-03-23 05:08:58
  • 解决tensorflow模型压缩的问题_踩坑无数,总算搞定

    2021-12-18 03:09:23
  • python分割一个文本为多个文本的方法

    2022-09-01 06:40:33
  • plsql与tsql的语法不同

    2009-09-13 17:33:00
  • Python利用Selenium实现网站自动签到功能

    2021-04-22 18:20:02
  • 使用Python处理Excel表格的简单方法

    2023-12-07 08:05:04
  • 如何使数据库中取出的数据保持原有格式

    2008-11-27 16:16:00
  • 手把手教你用python抢票回家过年(代码简单)

    2023-07-13 22:46:02
  • 解析:快速的掌握 MySQL支持的操作系统

    2008-12-31 17:18:00
  • Python中的Pandas 时间函数 time 、datetime 模块和时间处理基础讲解

    2022-08-01 05:18:41
  • php将textarea数据提交到mysql出现很多空格的解决方法

    2023-09-11 01:10:08
  • ASP 中 DateDiff 函数详解

    2007-09-19 12:00:00
  • 如何前后翻阅聊友们的发言?

    2010-01-18 20:49:00
  • asp之家 网络编程 m.aspxhome.com