A lightweight command-line utility to block and unblock domains at the OS level by manipulating the system hosts file. Works on Windows and Linux.
Blocking a domain adds entries to your system's hosts file that redirect both the bare domain and the www. variant to 127.0.0.1 (localhost), preventing your machine from reaching the real server. Unblocking removes those entries. On Windows, the DNS cache is flushed automatically after each operation.
- Python 3.x (no external dependencies — standard library only)
- Administrator / root privileges to modify the hosts file
python firewall.py --d <domain(s)> [--b | --u]
| Argument | Description |
|---|---|
--d, --domains |
One or more domains to act on (required) |
--b |
Block the specified domains |
--u |
Unblock the specified domains |
--b and --u are mutually exclusive — only one can be used per invocation.
# Block a single domain
python firewall.py --d example.com --b
# Block multiple domains at once
python firewall.py --d facebook.com twitter.com youtube.com --b
# Unblock a domain
python firewall.py --d example.com --uWindows — open PowerShell as Administrator, then run the script:
python firewall.py --d example.com --bLinux:
sudo python3 firewall.py --d example.com --bFor each domain, two entries are written:
127.0.0.1 example.com
127.0.0.1 www.example.com
| OS | Hosts file location |
|---|---|
| Windows | C:\Windows\System32\drivers\etc\hosts |
| Linux | /etc/hosts |
python-firewall/
└── firewall.py # Single-file implementation
- Does not block subdomains — only the bare domain and its
www.variant are blocked. Subdomains likemail.example.comorapi.example.comwill still be accessible. - DNS cache flushing (
ipconfig /flushdns) is Windows-only; on Linux you may need to flush manually or restart the browser. - Unblocking uses substring matching, so unblocking
mail.comcould affect any hosts entry that contains that string. - No validation that input strings are well-formed domain names.
- Does not check whether a domain is already blocked before blocking it.