久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

特定路由的燒瓶中間件

Flask middleware for specific route(特定路由的燒瓶中間件)
本文介紹了特定路由的燒瓶中間件的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我用 Python Flask-RESTful 制作了 API Server.

I made API Server with Python Flask-RESTful.

我的系統(tǒng)使用令牌認(rèn)證來(lái)驗(yàn)證權(quán)限.

My system use token authentication for verify permission.

所以,我添加了用于驗(yàn)證令牌的中間件.

So, I added middleware for verify token.

比如這樣的代碼,

[中間件.py]

class Test(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        print("gogo")
        return self.app(environ, start_response)

[app.py]

from flask import Flask
from flask_restful import Api
from api.board import Article
from api.auth import Login, Register, RefreshToken
from middleware import Test


app = Flask(__name__)
api = Api(app)

api.add_resource(Login, '/login')
api.add_resource(Register, '/register')
api.add_resource(RefreshToken, '/refresh')

# middleware here
app.wsgi_app = Test(app.wsgi_app)
api.add_resource(Article, '/article')

if __name__ == '__main__':
    app.run(debug=True)

我在 /article 之前插入 app.wsgi_app = Test(app.wsgi_app).

所以我希望只有訪問(wèn) /article 會(huì)打印 gogo",但是每條路線都會(huì)打印 gogo".

So I expect that only access to /article will print "gogo", however every route print "gogo".

也許每條路由都經(jīng)過(guò)中間件.

Maybe every route pass through with middleware.

如何為特定路由應(yīng)用中間件?(在這段代碼中,只有/article)

How can I apply middleware for specific route? (In this code, only /article)

推薦答案

有幾種方法可以在特定端點(diǎn)之前添加自定義處理.

There are a few ways how to add custom processing before specific endpoint.

1)使用python裝飾器:

1) Using python decorator:

from functools import wraps

def home_decorator():
    def _home_decorator(f):
        @wraps(f)
        def __home_decorator(*args, **kwargs):
            # just do here everything what you need
            print('before home')
            result = f(*args, **kwargs)
            print('home result: %s' % result)
            print('after home')
            return result
        return __home_decorator
    return _home_decorator


@app.route('/home')
@home_decorator()
def home():
    return 'Hello'

2) 使用 before_request

@app.before_request
def hook():
    # request - flask.request
    print('endpoint: %s, url: %s, path: %s' % (
        request.endpoint,
        request.url,
        request.path))
    # just do here everything what you need...

3) 使用中間件.只需在請(qǐng)求路徑上添加條件即可.

3) Using middleware. Just add condition on request path.

class Middleware:

    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        # not Flask request - from werkzeug.wrappers import Request
        request = Request(environ)
        print('path: %s, url: %s' % (request.path, request.url))
        # just do here everything what you need
        return self.app(environ, start_response)


@app.route('/home')
def home():
    return 'Hello'


app.wsgi_app = Middleware(app.wsgi_app)

4)您也可以使用before_request_funcs在特定藍(lán)圖之前設(shè)置函數(shù)列表.

4) Also you can use before_request_funcs to set list of functions before specific blueprint.

api = Blueprint('my_blueprint', __name__)


def before_my_blueprint():
    print(111)


@api.route('/test')
def test():
    return 'hi'


app.before_request_funcs = {
    # blueprint name: [list_of_functions]
    'my_blueprint': [before_my_blueprint]
}

app.register_blueprint(api)

希望這會(huì)有所幫助.

這篇關(guān)于特定路由的燒瓶中間件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How should I verify a log message when testing Python code under nose?(在鼻子下測(cè)試 Python 代碼時(shí),我應(yīng)該如何驗(yàn)證日志消息?)
Patch __call__ of a function(修補(bǔ)函數(shù)的 __call__)
How to call self in a mock method of an object in Python?(如何在 Python 中對(duì)象的模擬方法中調(diào)用 self?)
Mocking only a single method on an object(僅模擬對(duì)象上的單個(gè)方法)
Mocking a subprocess call in Python(在 Python 中模擬子進(jìn)程調(diào)用)
Checking call order across multiple mocks(檢查多個(gè)模擬的調(diào)用順序)
主站蜘蛛池模板: 激情婷婷综合 | 久久精视频 | 岛国av噜噜噜久久久狠狠av | 成人深夜福利视频 | 第一福利视频导航 | 久久精品99久久久久久 | 狠狠干狠狠干 | 免费一级a毛片 | 理论片中文字幕 | 日本久久精品视频 | 三级视频在线播放 | 久久免费看 | 亚洲欧美在线观看 | av网站观看 | 伊人干综合 | 亚洲91精品 | 欧美三级精品 | 精品久久久久久久久久久 | www.国产精品.com | 日韩一级在线观看 | 一区二区三区色 | 五月婷婷综合激情 | 国产一区二区在线播放 | 国产a久久麻豆入口 | 精品一区二区三区免费看 | 欧美一级做性受免费大片免费 | 国产精品久久久精品 | 欧美一区免费 | 一区二区三区四区在线播放 | 激情高潮到大叫狂喷水 | 日韩女优在线观看 | 亚洲天堂一区二区三区 | 成人午夜激情视频 | 国产第一福利 | 日韩在线观看一区 | 成人国产在线 | 亚洲人成在线播放 | 久久久在线 | 欧美日韩一二三 | 日韩一区二区免费视频 | 亚洲国产第一页 |