当前位置: 首页>>代码示例>>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;未经允许,请勿转载。