當前位置: 首頁>>代碼示例>>Python>>正文


Python flask_limiter.Limiter方法代碼示例

本文整理匯總了Python中flask_limiter.Limiter方法的典型用法代碼示例。如果您正苦於以下問題:Python flask_limiter.Limiter方法的具體用法?Python flask_limiter.Limiter怎麽用?Python flask_limiter.Limiter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flask_limiter的用法示例。


在下文中一共展示了flask_limiter.Limiter方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_invalid_decorated_static_limits

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def test_invalid_decorated_static_limits(caplog):
    app = Flask(__name__)
    limiter = Limiter(
        app, default_limits=["1/second"], key_func=get_remote_address
    )

    @app.route("/t1")
    @limiter.limit("2/sec")
    def t1():
        return "42"

    with app.test_client() as cli:
        with hiro.Timeline().freeze():
            assert cli.get("/t1").status_code == 200
            assert cli.get("/t1").status_code == 429
    assert (
        "failed to configure"
        in caplog.records[0].msg
    )
    assert (
        "exceeded at endpoint"
        in caplog.records[1].msg
    ) 
開發者ID:alisaifee,項目名稱:flask-limiter,代碼行數:25,代碼來源:test_decorators.py

示例2: test_invalid_decorated_dynamic_limits_blueprint

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def test_invalid_decorated_dynamic_limits_blueprint(caplog):
    app = Flask(__name__)
    app.config.setdefault("X", "2 per sec")
    limiter = Limiter(
        app, default_limits=["1/second"], key_func=get_remote_address
    )
    bp = Blueprint("bp1", __name__)

    @bp.route("/t1")
    def t1():
        return "42"

    limiter.limit(lambda: current_app.config.get("X"))(bp)
    app.register_blueprint(bp)

    with app.test_client() as cli:
        with hiro.Timeline().freeze():
            assert cli.get("/t1").status_code == 200
            assert cli.get("/t1").status_code == 429

    assert len(caplog.records) == 3
    assert "failed to load ratelimit" in caplog.records[0].msg
    assert "failed to load ratelimit" in caplog.records[1].msg
    assert "exceeded at endpoint" in caplog.records[2].msg 
開發者ID:alisaifee,項目名稱:flask-limiter,代碼行數:26,代碼來源:test_blueprints.py

示例3: __init__

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def __init__(self,global_limits=["1000/minute"]):
        self.app = Flask(__name__)
        self.global_limits = global_limits
        self.limiter = Limiter(self.app,key_func=get_ipaddr,global_limits=self.global_limits) 
開發者ID:wylok,項目名稱:sparrow,代碼行數:6,代碼來源:conf.py

示例4: init

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def init(app):

    limiter = Limiter(
        app,
        key_func=get_remote_address,
        default_limits=[]
    )
    return limiter 
開發者ID:bitshares,項目名稱:bitshares-explorer-api,代碼行數:10,代碼來源:limiter.py

示例5: test_invalid_decorated_dynamic_limits

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def test_invalid_decorated_dynamic_limits(caplog):
    app = Flask(__name__)
    app.config.setdefault("X", "2 per sec")
    limiter = Limiter(
        app, default_limits=["1/second"], key_func=get_remote_address
    )

    @app.route("/t1")
    @limiter.limit(lambda: current_app.config.get("X"))
    def t1():
        return "42"

    with app.test_client() as cli:
        with hiro.Timeline().freeze():
            assert cli.get("/t1").status_code == 200
            assert cli.get("/t1").status_code == 429
    # 2 for invalid limit, 1 for warning.
    assert len(caplog.records) == 3
    assert (
        "failed to load ratelimit"
        in caplog.records[0].msg
    )
    assert (
        "failed to load ratelimit"
        in caplog.records[1].msg
    )
    assert (
        "exceeded at endpoint"
        in caplog.records[2].msg
    )
    assert caplog.records[2].levelname == 'WARNING' 
開發者ID:alisaifee,項目名稱:flask-limiter,代碼行數:33,代碼來源:test_decorators.py

示例6: test_conditional_limits

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def test_conditional_limits():
    """Test that the conditional activation of the limits work."""
    app = Flask(__name__)
    limiter = Limiter(app, key_func=get_remote_address)

    @app.route("/limited")
    @limiter.limit("1 per day")
    def limited_route():
        return "passed"

    @app.route("/unlimited")
    @limiter.limit("1 per day", exempt_when=lambda: True)
    def never_limited_route():
        return "should always pass"

    is_exempt = False

    @app.route("/conditional")
    @limiter.limit("1 per day", exempt_when=lambda: is_exempt)
    def conditionally_limited_route():
        return "conditional"

    with app.test_client() as cli:
        assert cli.get("/limited").status_code == 200
        assert cli.get("/limited").status_code == 429

        assert cli.get("/unlimited").status_code == 200
        assert cli.get("/unlimited").status_code == 200

        assert cli.get("/conditional").status_code == 200
        assert cli.get("/conditional").status_code == 429
        is_exempt = True
        assert cli.get("/conditional").status_code == 200
        is_exempt = False
        assert cli.get("/conditional").status_code == 429 
開發者ID:alisaifee,項目名稱:flask-limiter,代碼行數:37,代碼來源:test_decorators.py

示例7: test_conditional_shared_limits

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def test_conditional_shared_limits():
    """Test that conditional shared limits work."""
    app = Flask(__name__)
    limiter = Limiter(app, key_func=get_remote_address)

    @app.route("/limited")
    @limiter.shared_limit("1 per day", "test_scope")
    def limited_route():
        return "passed"

    @app.route("/unlimited")
    @limiter.shared_limit(
        "1 per day", "test_scope", exempt_when=lambda: True
    )
    def never_limited_route():
        return "should always pass"

    is_exempt = False

    @app.route("/conditional")
    @limiter.shared_limit(
        "1 per day", "test_scope", exempt_when=lambda: is_exempt
    )
    def conditionally_limited_route():
        return "conditional"

    with app.test_client() as cli:
        assert cli.get("/unlimited").status_code == 200
        assert cli.get("/unlimited").status_code == 200

        assert cli.get("/limited").status_code == 200
        assert cli.get("/limited").status_code == 429

        assert cli.get("/conditional").status_code == 429
        is_exempt = True
        assert cli.get("/conditional").status_code == 200
        is_exempt = False
        assert cli.get("/conditional").status_code == 429 
開發者ID:alisaifee,項目名稱:flask-limiter,代碼行數:40,代碼來源:test_decorators.py

示例8: test_whitelisting

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def test_whitelisting():

    app = Flask(__name__)
    limiter = Limiter(
        app,
        default_limits=["1/minute"],
        headers_enabled=True,
        key_func=get_remote_address
    )

    @app.route("/")
    def t():
        return "test"

    @limiter.request_filter
    def w():
        if request.headers.get("internal", None) == "true":
            return True
        return False

    with hiro.Timeline().freeze() as timeline:
        with app.test_client() as cli:
            assert cli.get("/").status_code == 200
            assert cli.get("/").status_code == 429
            timeline.forward(60)
            assert cli.get("/").status_code == 200

            for i in range(0, 10):
                assert cli.get(
                    "/", headers={"internal": "true"}
                ).status_code == 200 
開發者ID:alisaifee,項目名稱:flask-limiter,代碼行數:33,代碼來源:test_decorators.py

示例9: test_invalid_decorated_static_limit_blueprint

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def test_invalid_decorated_static_limit_blueprint(caplog):
    app = Flask(__name__)
    limiter = Limiter(
        app, default_limits=["1/second"], key_func=get_remote_address
    )
    bp = Blueprint("bp1", __name__)

    @bp.route("/t1")
    def t1():
        return "42"

    limiter.limit("2/sec")(bp)
    app.register_blueprint(bp)

    with app.test_client() as cli:
        with hiro.Timeline().freeze():
            assert cli.get("/t1").status_code == 200
            assert cli.get("/t1").status_code == 429
    assert (
        "failed to configure"
        in caplog.records[0].msg
    )
    assert (
        "exceeded at endpoint"
        in caplog.records[1].msg
    ) 
開發者ID:alisaifee,項目名稱:flask-limiter,代碼行數:28,代碼來源:test_blueprints.py

示例10: wrap

# 需要導入模塊: import flask_limiter [as 別名]
# 或者: from flask_limiter import Limiter [as 別名]
def wrap(app, rule):
    """Wrap flask app with rule based request rate control.
    """
    limit_by = rule.pop('_limit_by', None)
    limiter = flask_limiter.Limiter(
        app,
        key_func=_get_key_func(limit_by=limit_by),
    )

    limit_value = rule.pop('_global', None)
    if limit_value is not None:
        decorator = limiter.shared_limit(limit_value, scope='_global')
        for endpoint, func in app.view_functions.items():
            # A shared rate limit could be evaluated multiple times for single
            # request, because `flask_limiter` uses
            # "func.__module__ + func.__name__" as key format to differentiate
            # non Blueprint routes.
            #
            # For example, both cases blow register to the same key format as
            # "flask.helpers.send_static_file"
            # 1. `restful_plus` static file requests (i.e. "/swaggerui/ui.js").
            # 2. default static file endpoint registration of
            # `treadmill.rest.FLASK_APP`, because param "static_folder" in
            # constructer has its default value.
            #
            # so each request of hitting "flask.helpers.send_static_file" will
            # increase rate limit counter by 2 (which 1 is expectation). A
            # workaround here is only concerning about `treadmill.rest.api`
            # resources.
            if hasattr(func, 'view_class'):
                app.view_functions[endpoint] = decorator(func)

    if rule:
        decorators = {
            module: limiter.shared_limit(limit_value, scope=module)
            for module, limit_value in rule.items()
        }

        for endpoint, func in app.view_functions.items():

            module = None
            if hasattr(func, 'view_class'):
                module = func.__module__.rsplit('.')[-1]

            if module in decorators:
                decorator = decorators[module]
                app.view_functions[endpoint] = decorator(func)

    return app 
開發者ID:Morgan-Stanley,項目名稱:treadmill,代碼行數:51,代碼來源:limit.py


注:本文中的flask_limiter.Limiter方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。