A Django app that implements IP-based blocklisting.
Project description
Django-blocklist
This is a Django app that implements IP-based blocklisting. Its BlocklistMiddleware
performs the blocking, and its clean_blocklist
management command deletes entries which have satisfied the cooldown period. Entries also have a reason
field, used in reporting. There are utility functions to add/remove IPs, an admin, and several management commands.
This app is primarily for situations where server-level blocking is not available, e.g. on platform-as-a-service hosts like PythonAnywhere or Heroku. Being an application-layer solution, it's not as performant as blocking via firewall or web server process, but is suitable for moderate traffic sites. It also offers better integration with the application stack, for easier management.
Quick start
-
The PyPI package name is
django-blocklist
; add that to yourrequirements.txt
or otherwise install it into your project's Python environment. -
Add "django_blocklist" to settings.INSTALLED_APPS
-
Add "django_blocklist.middleware.BlocklistMiddleware" to settings.MIDDLEWARE
-
Run
python manage.py migrate
to create thedjango_blocklist_blockedip
table. -
Add IPs to the list (via management commands,
utils.add_to_blocklist
, or the admin). -
Set up a cron job or equivalent to run
manage.py clean_blocklist
daily.
Management commands
Django-blocklist includes several management commands:
clean_blocklist
— remove entries that have fulfilled their cooldown periodimport_blocklist
— convenience command for importing IPs from a fileremove_from_blocklist
— remove one or more IPsreport_blocklist
— information on the current entriessearch_blocklist
— look for an IP in the list; in addition to info on stdout, returns an exit code of 0 if successfulupdate_blocklist
— add/update one or more IPs, with optional--reason
and--cooldown
values
The --help
for each of these details its available options.
For exporting or importing BlockedIP entries, use Django's built-in dumpdata
and loaddata
management commands.
Configuration
You can customize the following settings via a BLOCKLIST_CONFIG
dict in your project settings:
cooldown
— Days to expire, for new entries; default 7cache-ttl
— Seconds to cache the list of blocked IPs; default 60denial-template
— For the denial response; an f-string with{ip}
and{cooldown}
placeholders
Reporting
The report_blocklist
command gives summary information about the current collection of IPs, including how many requests from those IPs have been blocked. See the sample report for more.
Utility methods
The utils
module defines two convenience functions for updating the list from your application code:
add_to_blocklist(ips: set, reason, cooldown)
adds IPs to the blocklist (reason
andcooldown
are optional)remove_from_blocklist(ip: str)
removes an entry, returningTrue
if successful
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_blocklist-2.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9374c031a8108fc7eb7f3a9c61a86d0107407044d6f89b9ec6f66caee8195358 |
|
MD5 | 1085094b310f51e92c6d794fafea8a22 |
|
BLAKE2b-256 | 5cb88661af35431db67f87df75a218382af80bc2af50efa5f18f914749c1aba9 |