本文整理汇总了Python中flask_limiter.util.get_remote_address方法的典型用法代码示例。如果您正苦于以下问题:Python util.get_remote_address方法的具体用法?Python util.get_remote_address怎么用?Python util.get_remote_address使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask_limiter.util
的用法示例。
在下文中一共展示了util.get_remote_address方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_invalid_decorated_static_limits
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [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
)
示例2: test_reuse_logging
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def test_reuse_logging():
app = Flask(__name__)
app_handler = mock.Mock()
app_handler.level = logging.INFO
app.logger.addHandler(app_handler)
limiter = Limiter(app, key_func=get_remote_address)
for handler in app.logger.handlers:
limiter.logger.addHandler(handler)
@app.route("/t1")
@limiter.limit("1/minute")
def t1():
return "42"
with app.test_client() as cli:
cli.get("/t1")
cli.get("/t1")
assert app_handler.handle.call_count == 1
示例3: test_headers_breach
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def test_headers_breach():
app = Flask(__name__)
limiter = Limiter(
app,
default_limits=["10/minute"],
headers_enabled=True,
key_func=get_remote_address
)
@app.route("/t1")
@limiter.limit("2/second; 10 per minute; 20/hour")
def t():
return "test"
with hiro.Timeline().freeze() as timeline:
with app.test_client() as cli:
for i in range(11):
resp = cli.get("/t1")
timeline.forward(1)
assert resp.headers.get('X-RateLimit-Limit') == '10'
assert resp.headers.get('X-RateLimit-Remaining') == '0'
assert resp.headers.get('X-RateLimit-Reset') == \
str(int(time.time() + 50))
assert resp.headers.get('Retry-After') == str(int(50))
示例4: test_retry_after_exists_seconds
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def test_retry_after_exists_seconds():
app = Flask(__name__)
_ = Limiter(
app,
default_limits=["1/minute"],
headers_enabled=True,
key_func=get_remote_address
)
@app.route("/t1")
def t():
return "", 200, {'Retry-After': '1000000'}
with app.test_client() as cli:
resp = cli.get("/t1")
retry_after = int(resp.headers.get('Retry-After'))
assert retry_after > 1000
示例5: test_retry_after_exists_rfc1123
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def test_retry_after_exists_rfc1123():
app = Flask(__name__)
_ = Limiter(
app,
default_limits=["1/minute"],
headers_enabled=True,
key_func=get_remote_address
)
@app.route("/t1")
def t():
return "", 200, {'Retry-After': 'Sun, 06 Nov 2032 01:01:01 GMT'}
with app.test_client() as cli:
resp = cli.get("/t1")
retry_after = int(resp.headers.get('Retry-After'))
assert retry_after > 1000
示例6: test_custom_headers_from_setter_and_config
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def test_custom_headers_from_setter_and_config():
app = Flask(__name__)
app.config.setdefault(C.HEADER_LIMIT, "Limit")
app.config.setdefault(C.HEADER_REMAINING, "Remaining")
app.config.setdefault(C.HEADER_RESET, "Reset")
limiter = Limiter(
default_limits=["10/minute"],
headers_enabled=True,
key_func=get_remote_address
)
limiter._header_mapping[HEADERS.REMAINING] = 'Available'
limiter.init_app(app)
@app.route("/t1")
def t():
return "test"
with app.test_client() as cli:
for i in range(11):
resp = cli.get("/t1")
assert resp.headers.get('Limit') == '10'
assert resp.headers.get('Available') == '0'
assert resp.headers.get('Reset') is not None
示例7: init
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def init(app):
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=[]
)
return limiter
示例8: _get_key_func
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def _get_key_func(limit_by=None):
"""Get limit key factory function."""
def generate_limit_key():
"""Generate key for request rate limiting."""
# use authenticated user if specified, otherwise use ip by default
if limit_by == 'user' and flask.g.get('user') is not None:
return flask.g.get('user')
return util.get_remote_address()
return generate_limit_key
示例9: get
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def get(self):
# feeds get large need to force some cleanup
gc.collect()
if get_remote_address() != request.access_route[-1]:
return jsonify_unauth()
if not HTTPD_TOKEN:
return jsonify_success()
remote = ROUTER_ADDR
if current_app.config.get('CIF_ROUTER_ADDR'):
remote = current_app.config['CIF_ROUTER_ADDR']
try:
r = Client(remote, HTTPD_TOKEN).ping()
r = Client(remote, HTTPD_TOKEN).indicators_search({'indicator': 'example.com', 'nolog': '1'})
r = True
except TimeoutError:
return jsonify_unknown(msg='timeout', code=408)
except AuthError:
return jsonify_unauth()
if not r:
return jsonify_unknown(503)
return jsonify_success()
示例10: proxy_get_remote_address
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def proxy_get_remote_address():
if HTTPD_PROXY in ['1', 1]:
return request.access_route[-1]
return get_remote_address()
示例11: test_invalid_decorated_dynamic_limits
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [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'
示例12: test_conditional_limits
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [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
示例13: test_conditional_shared_limits
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [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
示例14: test_whitelisting
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [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
示例15: test_logging
# 需要导入模块: from flask_limiter import util [as 别名]
# 或者: from flask_limiter.util import get_remote_address [as 别名]
def test_logging(caplog):
app = Flask(__name__)
limiter = Limiter(app, key_func=get_remote_address)
@app.route("/t1")
@limiter.limit("1/minute")
def t1():
return "test"
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 429 == cli.get("/t1").status_code
assert len(caplog.records) == 1
assert caplog.records[0].levelname == 'WARNING'