Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Blueprint, Flask
- from flask_limiter import Limiter
- from flask_limiter.util import get_ipaddr as get_remote_address
- from flask_restful import Api, Resource
- class Config:
- RATELIMIT_DEFAULT = "1/3second"
- RATELIMIT_STORAGE_URL = "redis://"
- RATELIMIT_HEADERS_ENABLED = True
- RATELIMIT_IN_MEMORY_FALLBACK = "1/2second"
- RATELIMIT_KEY_PREFIX = "test-limiter"
- RATELIMIT_SWALLOW_ERRORS = True
- app = Flask(__name__)
- app.config.from_object(Config)
- limiter = Limiter(app, key_func=get_remote_address)
- api_bp = Blueprint("api", __name__)
- api = Api(api_bp)
- app.register_blueprint(api_bp, url_prefix="/api")
- class MyResource(Resource):
- decorators = [
- limiter.limit("3/minute", key_func=get_remote_address, methods=["GET"]),
- limiter.limit("1/3second", key_func=get_remote_address, methods=["POST"])
- ]
- def get(self):
- print("GET")
- return {"m": "get"}
- def post(self):
- print("POST")
- return {"m": "post"}
- api.add_resource(MyResource, "/my", endpoint="my")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement