Created
April 25, 2025 20:54
-
-
Save stuzero/0c05195823f712e7be2ab7c92b9ea52c to your computer and use it in GitHub Desktop.
Starlette Bot Handling
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
# See: https://datatracker.ietf.org/doc/html/rfc2324 (HTCPCP/1.0) | |
async def bot_detection_middleware(request: Request, call_next): | |
is_bot_request = await is_bot(request) | |
if datetime.now().month == 4 and datetime.now().day == 1 and is_bot_request: | |
return JSONResponse({"message": "No coffee"}, status_code=418) | |
elif is_bot_request: | |
return JSONResponse({"message": "Too Many Requests"}, status_code=429) | |
response = await call_next(request) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment