Python flask_limiter.util.get_remote_address() Examples
The following are 21
code examples of flask_limiter.util.get_remote_address().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
flask_limiter.util
, or try the search function
.
Example #1
Source File: limit.py From treadmill with Apache License 2.0 | 6 votes |
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
Example #2
Source File: test_flask_ext.py From flask-limiter with MIT License | 6 votes |
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
Example #3
Source File: test_flask_ext.py From flask-limiter with MIT License | 6 votes |
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
Example #4
Source File: test_flask_ext.py From flask-limiter with MIT License | 6 votes |
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
Example #5
Source File: test_flask_ext.py From flask-limiter with MIT License | 6 votes |
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))
Example #6
Source File: test_flask_ext.py From flask-limiter with MIT License | 6 votes |
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
Example #7
Source File: test_decorators.py From flask-limiter with MIT License | 6 votes |
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 )
Example #8
Source File: test_decorators.py From flask-limiter with MIT License | 6 votes |
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'
Example #9
Source File: __init__.py From bearded-avenger with Mozilla Public License 2.0 | 6 votes |
def proxy_get_remote_address(): if HTTPD_PROXY in ['1', 1]: return request.access_route[-1] return get_remote_address()
Example #10
Source File: health.py From bearded-avenger with Mozilla Public License 2.0 | 6 votes |
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()
Example #11
Source File: test_decorators.py From flask-limiter with MIT License | 5 votes |
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
Example #12
Source File: test_decorators.py From flask-limiter with MIT License | 5 votes |
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
Example #13
Source File: test_decorators.py From flask-limiter with MIT License | 5 votes |
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
Example #14
Source File: test_flask_ext.py From flask-limiter with MIT License | 5 votes |
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'
Example #15
Source File: test_flask_ext.py From flask-limiter with MIT License | 5 votes |
def test_headers_no_breach(): app = Flask(__name__) limiter = Limiter( app, default_limits=["10/minute"], headers_enabled=True, key_func=get_remote_address ) @app.route("/t1") def t1(): return "test" @app.route("/t2") @limiter.limit("2/second; 5 per minute; 10/hour") def t2(): return "test" with hiro.Timeline().freeze(): with app.test_client() as cli: resp = cli.get("/t1") assert resp.headers.get('X-RateLimit-Limit') == '10' assert resp.headers.get('X-RateLimit-Remaining') == '9' assert resp.headers.get('X-RateLimit-Reset') == \ str(int(time.time() + 61)) assert resp.headers.get('Retry-After') == str(60) resp = cli.get("/t2") assert resp.headers.get('X-RateLimit-Limit') == '2' assert resp.headers.get('X-RateLimit-Remaining') == '1' assert resp.headers.get('X-RateLimit-Reset') == \ str(int(time.time() + 2)) assert resp.headers.get('Retry-After') == str(1)
Example #16
Source File: test_flask_ext.py From flask-limiter with MIT License | 5 votes |
def test_custom_headers_from_setter(): app = Flask(__name__) limiter = Limiter( app, default_limits=["10/minute"], headers_enabled=True, key_func=get_remote_address, retry_after='http-date' ) limiter._header_mapping[HEADERS.RESET] = 'X-Reset' limiter._header_mapping[HEADERS.LIMIT] = 'X-Limit' limiter._header_mapping[HEADERS.REMAINING] = 'X-Remaining' @app.route("/t1") @limiter.limit("2/second; 10 per minute; 20/hour") def t(): return "test" with hiro.Timeline().freeze(0) 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-Limit') == '10' assert resp.headers.get('X-Remaining') == '0' assert resp.headers.get( 'X-Reset' ) == str(int(time.time() + 50)) assert resp.headers.get( 'Retry-After' ) == 'Thu, 01 Jan 1970 00:01:01 GMT'
Example #17
Source File: test_flask_ext.py From flask-limiter with MIT License | 5 votes |
def test_custom_headers_from_config(): app = Flask(__name__) app.config.setdefault(C.HEADER_LIMIT, "X-Limit") app.config.setdefault(C.HEADER_REMAINING, "X-Remaining") app.config.setdefault(C.HEADER_RESET, "X-Reset") 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-Limit') == '10' assert resp.headers.get('X-Remaining') == '0' assert resp.headers.get( 'X-Reset' ) == str(int(time.time() + 50))
Example #18
Source File: limiter.py From bitshares-explorer-api with MIT License | 5 votes |
def init(app): limiter = Limiter( app, key_func=get_remote_address, default_limits=[] ) return limiter
Example #19
Source File: test_configuration.py From flask-limiter with MIT License | 5 votes |
def test_invalid_storage_string(): app = Flask(__name__) app.config.setdefault(C.STORAGE_URL, "fubar://localhost:1234") with pytest.raises(ConfigurationError): Limiter(app, key_func=get_remote_address)
Example #20
Source File: test_configuration.py From flask-limiter with MIT License | 5 votes |
def test_constructor_arguments_over_config(redis_connection): app = Flask(__name__) app.config.setdefault(C.STRATEGY, "fixed-window-elastic-expiry") limiter = Limiter( strategy='moving-window', key_func=get_remote_address ) limiter.init_app(app) app.config.setdefault(C.STORAGE_URL, "redis://localhost:36379") assert type(limiter._limiter) == MovingWindowRateLimiter limiter = Limiter( storage_uri='memcached://localhost:31211', key_func=get_remote_address ) limiter.init_app(app) assert type(limiter._storage) == MemcachedStorage
Example #21
Source File: test_flask_ext.py From flask-limiter with MIT License | 4 votes |
def test_multiple_apps(): app1 = Flask(__name__) app2 = Flask(__name__) limiter = Limiter( default_limits=["1/second"], key_func=get_remote_address ) limiter.init_app(app1) limiter.init_app(app2) @app1.route("/ping") def ping(): return "PONG" @app1.route("/slowping") @limiter.limit("1/minute") def slow_ping(): return "PONG" @app2.route("/ping") @limiter.limit("2/second") def ping_2(): return "PONG" @app2.route("/slowping") @limiter.limit("2/minute") def slow_ping_2(): return "PONG" with hiro.Timeline().freeze() as timeline: with app1.test_client() as cli: assert cli.get("/ping").status_code == 200 assert cli.get("/ping").status_code == 429 timeline.forward(1) assert cli.get("/ping").status_code == 200 assert cli.get("/slowping").status_code == 200 timeline.forward(59) assert cli.get("/slowping").status_code == 429 timeline.forward(1) assert cli.get("/slowping").status_code == 200 with app2.test_client() as cli: assert cli.get("/ping").status_code == 200 assert cli.get("/ping").status_code == 200 assert cli.get("/ping").status_code == 429 timeline.forward(1) assert cli.get("/ping").status_code == 200 assert cli.get("/slowping").status_code == 200 timeline.forward(59) assert cli.get("/slowping").status_code == 200 assert cli.get("/slowping").status_code == 429 timeline.forward(1) assert cli.get("/slowping").status_code == 200