-
I am working on a Flask app for fun and wanted to set random wait limits between subsequent requests. I looked into the docs and source for a way to set random limits, but couldn't find any. Something like below:
|
Beta Was this translation helpful? Give feedback.
Answered by
alisaifee
Jun 2, 2023
Replies: 1 comment
-
There's nothing built in that would support this, but you could use your own import random
from flask import Flask
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
app = Flask(__name__)
limiter = Limiter(
get_remote_address, app=app, default_limits=["20/day"]
)
@app.after_request
def custom_headers(response):
if limiter.current_limit:
response.headers[
"X-RateLimit-Reset"
] = limiter.current_limit.reset_at + random.randint(1, 10)
response.headers["X-RateLimit-Remaining"] = max(
0, limiter.current_limit.remaining - random.randint(1, 10)
)
return response
@app.route("/")
def root():
return "42"
app.run() |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
alisaifee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's nothing built in that would support this, but you could use your own
after_request
hook to populate the headers yourself. Something like this might work: