This project is a Python-based website blocker that restricts access to selected websites during specified working hours. It modifies the system's hosts file to redirect targeted domains to the local machine, effectively preventing access.
The script supports both Unix-based systems and Windows by dynamically detecting the operating system and selecting the appropriate hosts file.
- Cross-platform support (Linux, macOS, Windows)
- Time-based website blocking
- Interactive selection of popular websites
- Support for custom user-defined domains
- Automatic cleanup of hosts file on exit
- Error handling for insufficient permissions
- Modular, object-oriented design
The program modifies the system's hosts file by adding entries that redirect selected domains (e.g., facebook.com) to 127.0.0.1. This prevents the browser from reaching the actual website.
During non-working hours, the script restores the hosts file by removing those entries.
This could be very useful for system administrators, parents, or anyone looking to improve productivity by limiting access to distracting websites during certain hours.
Python 3.x Administrator/root privileges (required to modify the hosts file)
- Run the script
On Linux/macOS:
sudo python3 web_blocker.pyOn Windows (run terminal as Administrator):
python web_blocker.py- Select websites You will be prompted to:
- Choose from a list of common websites
- Or enter custom domains
- Set working hours Enter:
- Start hour (0–23)
- End hour (0–23)
If invalid input is provided, default values (8–16) will be used.
If you choose for example facebook.com to block, the script will add entries like to the hosts file:
127.0.0.1 facebook.com
127.0.0.1 www.facebook.com
- When running in blocking hours
- When blocking hours finished -> unblocking websites
- The script must be run with administrator/root privileges.
- Modifying the hosts file affects system-wide network behavior.
- The program automatically restores the hosts file when it is stopped using Ctrl+C.
- Refactored into an object-oriented design
- Added cross-platform compatibility
- Introduced interactive user input
- Improved timing responsiveness
- Added safe cleanup on exit
- Improved error handling and reliability
- Command-line arguments support
- Configuration file for persistent settings
- Logging system
- Unit testing
- Instead of the program print status even if nothing changed, the program remembers its state and only acts when something actually changes.
- Admin / Root Check (Early Exit)
- Added a
check_admin()function. - Script now verifies permissions before doing anything.
- If not run as admin/root → exits immediately with a clear message.
- State-Based Execution (No Spam)
-
Introduced
self.is_blockedas a state flag. -
Script now:
- ✅ Prints only when state changes
- ❌ No repeated "Blocking..." / "Unblocking..." messages
-
Behavior is now event-driven, not repetitive polling output.
- Initial State Synchronization
-
On startup, script immediately:
- Checks current time
- Applies correct state (block/unblock)
- Prints status once
- Safe Cleanup on Exit (Ctrl + C)
-
Added
cleanup()handler usingsignal -
On exit:
- Automatically unblocks websites
- Prevents leaving system in blocked state
-
Ensures safe and clean shutdown
- Improved Error Handling
-
Centralized permission error handling (
admin_error()) -
Added:
- Graceful handling for
PermissionError - Catch-all for unexpected exceptions
- Graceful handling for
-
Prevents crashes → shows user-friendly messages
- Loop Optimization
-
Changed polling interval:
- From frequent checks →
time.sleep(60)
- From frequent checks →
-
Reduces unnecessary CPU usage and file operations
- Terminal Output Improvements
-
Kept ANSI colors for:
- Errors (red)
- Success (green)
- Info (yellow/cyan)
-
Output is now cleaner and more meaningful
-
Added validation for:
- Hour inputs (0–23)
-
Falls back to defaults if input is invalid
The script is now:
- ✔ More robust
- ✔ More user-friendly
- ✔ More efficient
- ✔ Safer to run (no leftover blocking)
- ✔ Structured like a real background service
State-driven system service (mini daemon)
Instead of blindly looping, it:
- Detects changes
- Reacts only when needed
- Maintains consistent system state
This project is part of a public repository intended for educational and practical use.



