Advertisement
cmiN

lim.py

Feb 2nd, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. from flask import Blueprint, Flask
  2. from flask_limiter import Limiter
  3. from flask_limiter.util import get_ipaddr as get_remote_address
  4. from flask_restful import Api, Resource
  5.  
  6.  
  7. class Config:
  8.     RATELIMIT_DEFAULT = "1/3second"
  9.     RATELIMIT_STORAGE_URL = "redis://"
  10.     RATELIMIT_HEADERS_ENABLED = True
  11.     RATELIMIT_IN_MEMORY_FALLBACK = "1/2second"
  12.     RATELIMIT_KEY_PREFIX = "test-limiter"
  13.     RATELIMIT_SWALLOW_ERRORS = True
  14.  
  15.  
  16. app = Flask(__name__)
  17. app.config.from_object(Config)
  18.  
  19. limiter = Limiter(app, key_func=get_remote_address)
  20.  
  21. api_bp = Blueprint("api", __name__)
  22. api = Api(api_bp)
  23. app.register_blueprint(api_bp, url_prefix="/api")
  24.  
  25.  
  26. class MyResource(Resource):
  27.  
  28.     decorators = [
  29.        limiter.limit("3/minute", key_func=get_remote_address, methods=["GET"]),
  30.        limiter.limit("1/3second", key_func=get_remote_address, methods=["POST"])
  31.     ]
  32.  
  33.     def get(self):
  34.         print("GET")
  35.         return {"m": "get"}
  36.  
  37.     def post(self):
  38.         print("POST")
  39.         return {"m": "post"}
  40.  
  41.  
  42. api.add_resource(MyResource, "/my", endpoint="my")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement