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.txtor 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 migrateto create thedjango_blocklist_blockediptable. -
Add IPs to the list (via management commands,
utils.update_blocklist, or the admin). -
Set up a cron job or equivalent to run
manage.py clean_blocklistdaily.
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 fileprint_blocklist— print list of blocked IPs in plain text or JSON formremove_from_blocklist— remove one or more IPsreport_blocklist— information on the current entries (see the sample report)search_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 IPs;--skip-existingprevents updating existing records
See --help for each of these for more details.
Configuration
You can customize the following settings via a BLOCKLIST_CONFIG dict in your project settings:
cooldown— Days to expire, for new entries; default 7denial-template— For the denial response; an f-string with{ip}and{cooldown}placeholders
Utility methods
The utils module defines some convenience functions:
remove_from_blocklist(ip: str)— removes an entry, returningTrueif successfulshould_block(request: HttpRequest)— Checks the request IP and method against the blocklist.update_blocklist(ips: set, reason: str, cooldown: int, last_seen: datetime)— adds IPs to the blocklist (all args exceptipsare optional)
Development
- Project hub: django-blocklist on Gitlab
- Development is managed with Poetry
- Quickstart:
python -m venv venv; source venv/bin/activate; poetry install - The project's settings.py is minimal, just enough to run the tests and the admin
- Run tests:
toxin project root to run the full test matrix, or usepytestto run specific tests under your installed Django - Linting:
ruff check django_blocklist manage.py runserverlets you try out the admin- Merge requests and issues (tickets) are welcome!
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_blocklist-3.2.1.tar.gz.
File metadata
- Download URL: django_blocklist-3.2.1.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.14.2 Darwin/25.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0d311b30583072d97aa1a8632afb4ae35953d42addc8edbba49a5da21600c3a
|
|
| MD5 |
5fe91f8b55a17bc61239a8bdb1dbf729
|
|
| BLAKE2b-256 |
4b63be001e3048a7cb18c05d762e64d65d620276d01448a7aa28f2933c79f0ec
|
File details
Details for the file django_blocklist-3.2.1-py3-none-any.whl.
File metadata
- Download URL: django_blocklist-3.2.1-py3-none-any.whl
- Upload date:
- Size: 27.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.14.2 Darwin/25.2.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8e7d065b52c31df696e62ce3fd8b20b6a31b295226c2cb5c0a6ceedc15b6a7b
|
|
| MD5 |
1432be4a054487844a90ea813ed509a6
|
|
| BLAKE2b-256 |
c0dac24e54bb27f1d945eb7c20da06422ce80c33b36c48a44056d2b12c85a0e1
|