python - Tryind to create custom error response for flask_limiter in a flask restful app
问题描述
I was trying to create a flask app with certain limits on a particular endpoint.
I have defined my app structure like so:
- run.py - >. to instantiate and run the flask server
class MyFlaskApp(Flask):
def run(self, host=None, port=None, debug=None, **options):
with self.app_context():
super(MyFlaskApp, self).run(host=host, port=port, debug=debug, **options)
app = MyFlaskApp(__name__)
CORS(app)
@app.errorhandler(429)
def ratelimit_handler(e):
return e, 209
if __name__ == "__main__":
app.run(host="0.0.0.0",port=5001, debug=True)
- limiter.py - > the ratelmiter config file containing.
@app.errorhandler(429)
def ratelimit_handler(e):
return e, 209
app = Flask(__name__)
limiter = Limiter(app, key_func=get_remote_address)
- service.py -> Using Flask Pluggable Views
class MyView(flask.views.MethodView):
decorators = [limiter.limit("10/second")]
def post(self):
return "get"
def someother_functions(self):
return ""
- app.py-> to include all the routes
api_bp = Blueprint('api', __name__)
api = Api(api_bp)
#Route
api.add_resource(service_file.service,'/getpost')
The rate limit seems to work properly but i the custom errorhandler doesn't seem to work. what i am trying to achieve is to capture 429 error code and return it as some other code in 2xx group because of some limitation on my server side, but this doesn;t seem to work even when i place it in the limiter.py file similarly. I have been stuck at this for hours!. Any help would be appreciated!.
解决方案
推荐阅读
- python - 如何优化回调python代码以获得更好的可读性和pythonic实践?
- azure-data-explorer - Kusto - 如果其他条件与 Kusto
- reactjs - 如何检查给定的字符串是字符串文字类型还是其他字符串值
- java - Completablefuture 在异常时未完成
- rest - 使用 Evo File API 上传文件时出现问题
- python - 使用 pandas groupby 查找列的唯一组合并另存为 df
- azure - Terraform Azure Databricks 提供程序错误
- next.js - next-redux-wrapper(6.0.2) 中的 getServersideProps 不起作用,但 getInitialProps 没问题
- python - VS Code“查找所有引用”不能忽略排除的文件
- javascript - 如何对一组对象进行分组并将其映射到不同的结构?