0
0
Flaskframework~10 mins

Flask-Limiter for rate limiting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask-Limiter extension.

Flask
from flask_limiter import [1]
Drag options to blanks, or click blank then click option'
ALimiter
BLimiterExtension
CRateLimiter
DLimiterTool
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like 'RateLimiter' or 'LimiterExtension'.
Trying to import from 'flask_limiter_extension' instead of 'flask_limiter'.
2fill in blank
medium

Complete the code to create a Flask app and initialize Limiter with a default rate limit of 5 per minute.

Flask
app = Flask(__name__)
limiter = Limiter(app, default_limits=["[1]"])
Drag options to blanks, or click blank then click option'
A5 per minute
B10 per second
C100 per hour
D1 per day
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers without quotes.
Using wrong time units like 'per seconds' instead of 'per second'.
3fill in blank
hard

Fix the error in the route decorator to apply a rate limit of 3 per minute.

Flask
@app.route("/")
@limiter.limit("[1]")
def home():
    return "Hello!"
Drag options to blanks, or click blank then click option'
A3 per second
B3 per hour
C3 per day
D3 per minute
Attempts:
3 left
💡 Hint
Common Mistakes
Using '3 per seconds' which is grammatically incorrect.
Using '3 per hour' which is a different rate.
4fill in blank
hard

Fill both blanks to create a rate limit key function that limits by remote address.

Flask
limiter = Limiter(app, key_func=[1], default_limits=["10 per minute"])

@app.route("/data")
@limiter.limit("5 per minute", key_func=[2])
def data():
    return "Data"
Drag options to blanks, or click blank then click option'
Aget_remote_address
Bremote_addr
Cclient_ip
Duser_ip
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names like 'remote_addr' instead of the function.
Using made-up names like 'client_ip' or 'user_ip'.
5fill in blank
hard

Fill all three blanks to create a custom error message for rate limit exceeded.

Flask
from flask import Flask, jsonify
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)
limiter = Limiter(app, key_func=get_remote_address, default_limits=["5 per minute"])

@app.errorhandler([1])
def ratelimit_handler(e):
    return jsonify(error="[2]"), [3]
Drag options to blanks, or click blank then click option'
A429
BRate limit exceeded. Try again later.
C429 Too Many Requests
D403
Attempts:
3 left
💡 Hint
Common Mistakes
Using 403 Forbidden instead of 429.
Returning a status code as a string instead of an integer.