如何使用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))看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。