IPBanManager¶
The IPBanManager class handles temporary IP bans in your Flask application.
Overview¶
The IPBanManager uses an in-memory cache to track banned IPs and their ban durations.
Distributed Banning¶
When Redis is enabled:
- Bans are shared across instances
- Ban expiration is handled automatically
- Supports atomic ban operations
Methods¶
ban_ip¶
Ban an IP address for a specified duration.
Parameters:
- ip: The IP address to ban
- duration: Ban duration in seconds
- reason: Reason for the ban (default: "threshold_exceeded")
Example:
is_ip_banned¶
Check if an IP address is currently banned.
Parameters:
- ip: The IP address to check
Returns:
- bool: True if the IP is banned, False otherwise
Example:
unban_ip¶
Remove a ban on an IP address.
Parameters:
- ip: The IP address to unban
Example:
reset¶
Reset all banned IPs.
Example:
Usage with FlaskAPIGuard¶
The IPBanManager is automatically integrated when you use the FlaskAPIGuard extension: