Skip to content

Instantly share code, notes, and snippets.

@stuzero
Created April 25, 2025 20:54
Show Gist options
  • Save stuzero/0c05195823f712e7be2ab7c92b9ea52c to your computer and use it in GitHub Desktop.
Save stuzero/0c05195823f712e7be2ab7c92b9ea52c to your computer and use it in GitHub Desktop.
Starlette Bot Handling
# 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