FlaskFloodgate.handlers#

Module contents#

class FlaskFloodgate.handlers.DBHandler#

Bases: ABC

The storage handler for the rate-limit handler. You can create your own custom subclass and use it accordingly.

Default Handlers provided#

TODO:

Add support for JSON.

abstractmethod classmethod blacklist_ip(ip: str, ddw: bool = True) None#

Used to blacklist an IP.

Parameters:
  • ip (str) – The IP to blacklist.

  • ddw (bool, optional) – Indicates whether to delete the IP data when it is blacklisted, defaults to True.

Raises:

NotImplementedError – Indicates that the custom subclass has not implemented this method.

abstractmethod classmethod de_blacklist_ip(ip: str) None#

Used to de-blacklist an IP.

Parameters:

ip (str) – The IP to de-blacklist.

Raises:

NotImplementedError – Indicates that the custom subclass has not implemented this method.

abstractmethod classmethod de_whitelist_ip(ip: str) None#

Used to de-whitelist an IP.

Parameters:

ip (str) – The IP to whitelist.

Raises:

NotImplementedError – Indicates that the custom subclass has not implemented this method.

abstractmethod classmethod get_ip(ip: str) IP | None#

Used to get an IP.

Parameters:

ip (str) – The IP to get.

Returns:

The retrieved IP or None (if not found).

Return type:

Union[IP, None]

Raises:

NotImplementedError – Indicates that custom subclass has not implemented this method.

abstractmethod classmethod is_blacklisted(ip: str) bool#

Used to check if an IP is blacklisted or not.

Parameters:

ip (str) – The IP to check.

Returns:

A boolean value indicating whether the IP is blacklisted or not.

Return type:

bool

Raises:

NotImplementedError – Indicates that custom subclass has not implemented this method.

abstractmethod classmethod is_whitelisted(ip: str) bool#

Used to check if an IP is whitelisted or not.

Parameters:

ip (str) – The IP to check.

Returns:

A boolean value indicating whether the IP is whitelisted or not.

Return type:

bool

Raises:

NotImplementedError – Indicates that custom subclass has not implemented this method.

abstractmethod classmethod save_ip(ip) None#

Used to save an IP.

Parameters:

ip (IP) – The IP to save.

Raises:

NotImplementedError – Indicates that custom subclass has not implemented this method.

abstractmethod classmethod whitelist_ip(ip: str, ddw: bool = True) None#

Used to whitelist an IP.

Parameters:
  • ip (str) – The IP to de-whitelist.

  • ddw (bool, optional) – Indicates whether to delete the IP data when it is whitelisted, defaults to True.

Raises:

NotImplementedError – Indicates that the custom subclass has not implemented this method.

class FlaskFloodgate.handlers.MemoryHandler#

Bases: DBHandler

A custom subclass of DBHandler. Represents a RAM / Memory Handler for IP-related data.

blacklist_ip(ip: str, ddw: bool = True)#

Used to blacklist an IP.

Parameters:
  • ip (str) – The IP to blacklist.

  • ddw (bool, optional) – Indicates whether to delete the IP data when it is blacklisted, defaults to True.

de_blacklist_ip(ip: str) None#

Used to de-blacklist an IP.

Parameters:

ip (str) – The IP to de-blacklist.

de_whitelist_ip(ip: str) None#

Used to de-whitelist an IP.

Parameters:

ip (str) – The IP to whitelist.

get_ip(ip: str)#

Used to get an IP.

Parameters:

ip (str) – The IP to get.

Returns:

The retrieved IP or None (if not found).

Return type:

Union[IP, None]

is_blacklisted(ip: str)#

Used to check if an IP is blacklisted or not.

Parameters:

ip (str) – The IP to check.

Returns:

A boolean value indicating whether the IP is blacklisted or not.

Return type:

bool

is_whitelisted(ip: str)#

Used to check if an IP is whitelisted or not.

Parameters:

ip (str) – The IP to check.

Returns:

A boolean value indicating whether the IP is whitelisted or not.

Return type:

bool

save_ip(ip: IP)#

Used to save an IP.

Parameters:

ip (IP) – The IP to save.

whitelist_ip(ip: str, ddw: bool = True)#

Used to whitelist an IP.

Parameters:
  • ip (str) – The IP to whitelist.

  • ddw (bool, optional) – Indicates whether to delete the IP data when it is whitelisted, defaults to True.

class FlaskFloodgate.handlers.RedisHandler(redis_url: str)#

Bases: DBHandler

A custom subclass of DBHandler. Represents a Redis Handler for IP-related data.

Parameters:

redis_url (str) – The URL of the redis connection.

blacklist_ip(ip: str, ddw: bool = True)#

Used to blacklist an IP.

Parameters:
  • ip (str) – The IP to blacklist.

  • ddw (bool, optional) – Indicates whether to delete the IP data when it is blacklisted, defaults to True.

de_blacklist_ip(ip: str) None#

Used to de-blacklist an IP.

Parameters:

ip (str) – The IP to de-blacklist.

de_whitelist_ip(ip: str) None#

Used to de-whitelist an IP.

Parameters:

ip (str) – The IP to whitelist.

get_ip(ip: str)#

Used to get an IP.

Parameters:

ip (str) – The IP to get.

Returns:

The retrieved IP or None (if not found).

Return type:

Union[IP, None]

is_blacklisted(ip: str)#

Used to check if an IP is blacklisted or not.

Parameters:

ip (str) – The IP to check.

Returns:

A boolean value indicating whether the IP is blacklisted or not.

Return type:

bool

is_whitelisted(ip: str)#

Used to check if an IP is whitelisted or not.

Parameters:

ip (str) – The IP to check.

Returns:

A boolean value indicating whether the IP is whitelisted or not.

Return type:

bool

save_ip(ip: IP)#

Used to save an IP.

Parameters:

ip (IP) – The IP to save.

whitelist_ip(ip: str, ddw: bool = True)#

Used to whitelist an IP.

Parameters:
  • ip (str) – The IP to whitelist.

  • ddw (bool, optional) – Indicates whether to delete the IP data when it is whitelisted, defaults to True.

class FlaskFloodgate.handlers.Sqlite3Handler(fp: str, table_name: str, extra_table_name: str, wal_mode: bool = False)#

Bases: DBHandler

A custom subclass of DBHandler. Represents an Sqlite3 Handler for IP-related data.

Parameters:
  • fp (str) – The file-path of the .db file.

  • table_name (str) – The name of the table.

  • extra_table_name (str) – The name of the extra table where the blacklist and whitelist data are stored.

  • wal_mode (bool, optional) – Indicates whether to set the journal_mode to wal, defaults to False.

blacklist_ip(ip: str, ddw: bool = True)#

Used to blacklist an IP.

Parameters:
  • ip (str) – The IP to blacklist.

  • ddw (bool, optional) – Indicates whether to delete the IP data when it is blacklisted, defaults to True.

de_blacklist_ip(ip: str) None#

Used to de-blacklist an IP.

Parameters:

ip (str) – The IP to de-blacklist.

de_whitelist_ip(ip: str) None#

Used to de-whitelist an IP.

Parameters:

ip (str) – The IP to whitelist.

get_ip(ip: str)#

Used to get an IP.

Parameters:

ip (str) – The IP to get.

Returns:

The retrieved IP or None (if not found).

Return type:

Union[IP, None]

is_blacklisted(ip: str)#

Used to check if an IP is blacklisted or not.

Parameters:

ip (str) – The IP to check.

Returns:

A boolean value indicating whether the IP is blacklisted or not.

Return type:

bool

is_whitelisted(ip: str)#

Used to check if an IP is whitelisted or not.

Parameters:

ip (str) – The IP to check.

Returns:

A boolean value indicating whether the IP is whitelisted or not.

Return type:

bool

save_ip(ip: IP)#

Used to save an IP.

Parameters:

ip (IP) – The IP to save.

whitelist_ip(ip: str, ddw: bool = True)#

Used to whitelist an IP.

Parameters:
  • ip (str) – The IP to whitelist.

  • ddw (bool, optional) – Indicates whether to delete the IP data when it is whitelisted, defaults to True.