Created
January 11, 2024 00:14
-
-
Save Armster15/46f8c16a8151becc3119e3c1ac755cf8 to your computer and use it in GitHub Desktop.
Flask app with rate limits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify | |
from flask_limiter import Limiter | |
from flask_limiter.util import get_remote_address | |
app = Flask(__name__) | |
# Configure rate limiting with in-memory storage | |
limiter = Limiter( | |
get_remote_address, | |
app=app, | |
storage_uri="memory://", | |
application_limits=["50 per 5 seconds"] | |
) | |
# API route with rate limit | |
@app.route('/', methods=['GET']) | |
@limiter.limit("50 per 5 seconds") | |
def get_resource(): | |
return jsonify({'message': 'This is your resource!'}) | |
if __name__ == '__main__': | |
app.run(debug=True, port=5050) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment