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 select:
- facebook.com
- youtube.com
The script will add entries like to the hosts file:
127.0.0.1 facebook.com
127.0.0.1 www.facebook.com- 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
This project is part of a public repository intended for educational and practical use.
