In the digital age, managing online distractions is crucial for maintaining productivity and safeguarding one’s online privacy.
The Python script we’ve discussed offers a straightforward solution for blocking unwanted websites directly from a user’s computer by modifying the system’s hosts file.
Utilizing Python’s tkinter
library, this script provides a simple graphical user interface (GUI), allowing users to easily add websites they wish to block.
Why This Code is Useful
This code is particularly useful for several reasons:
- Productivity: It helps individuals and organizations improve focus and productivity by blocking access to distracting websites.
- Parental Controls: Parents can use this tool to restrict access to inappropriate websites for their children.
- Privacy Protection: Blocking tracking sites or those known to distribute malware helps protect a user’s privacy and security.
Step 1: Install Required Libraries
First, we need to install Python libraries for creating the GUI. We will use tkinter
for the GUI (it’s included with Python, so no need to install it) and tkinter.simpledialog
for input dialogs.
If you haven’t installed Python yet, you need to install it from the Python official website.
Step 2: Creating the Python Script
Now, let’s create a Python script that can modify the system’s hosts file to block websites. The hosts file is used by an operating system to map hostnames to IP addresses.
- Open your code editor: Start by opening your favorite code editor (like VSCode, PyCharm, etc.).
- Create a new Python file: Save it as
website_blocker.py
.
Step 3: Write the Python Script
Import Libraries
import tkinter as tk
from tkinter import simpledialog
import os
tkinter
: This is the standard GUI toolkit for Python.simpledialog
: This is used to get input from the user through simple popup- dialogs.
os
: This module will help us modify the hosts file.
Define Hosts File Path and Redirect IP
HOSTS_PATH = "/etc/hosts" # This path is for Unix/Linux. Use C:\\Windows\\System32\\drivers\\etc\\hosts for Windows.
REDIRECT_IP = "127.0.0.1"
HOSTS_PATH
: Path to your system’s hosts file. The path provided is for Linux and macOS. For Windows, replace it with C:\\Windows\\System32\\drivers\\etc\\hosts
.
REDIRECT_IP
: We redirect blocked websites to this local IP, essentially ‘blocking’ them.
Create a Function to Block Websites
def block_website():
website = simpledialog.askstring("Input", "Enter website to block:", parent=root)
if website:
with open(HOSTS_PATH, 'a') as file:
file.write(f"{REDIRECT_IP} {website}\n")
label.config(text=f"{website} has been blocked.")
else:
label.config(text="No website entered.")
- This function asks the user to enter the website they want to block.
- It appends the website to the hosts file, redirecting it to the
REDIRECT_IP
. label.config(text=...)
updates the label based on whether a website was entered.
Set Up the GUI
root = tk.Tk()
root.title("Website Blocker")
label = tk.Label(root, text="Enter a website to block:", font=('Helvetica', 12))
label.pack(pady=20)
block_button = tk.Button(root, text="Block Website", command=block_website, font=('Helvetica', 12))
block_button.pack(pady=20)
root.mainloop()
root = tk.Tk()
: This creates the main window.root.title(...)
: Sets the title of the window.- A label and button are created and packed into the window.
root.mainloop()
: Starts the GUI event loop.
Step 4: Run the Script
To run your script:

- Open your terminal or command prompt.
- Navigate to the directory where your script is saved.
- Run the script by typing
python website_blocker.py
.
Additional Notes
- Administrator Privileges: You need to run the script with administrator privileges since it modifies the hosts file.
- Unblocking: To unblock, you would need to manually edit the hosts file and remove the entries you added.
Advantages of This Code
- Ease of Use: The GUI makes it very user-friendly, even for those with minimal technical expertise.
- Customizability: Users can easily modify the list of blocked websites as needed.
- No Dependency on External Services: Unlike browser extensions or third-party software, this script does not rely on any external services, keeping the control in the user’s hands.
Disadvantages of This Code
- Limited by User Access Rights: The script needs to be run with administrator privileges to modify the hosts file, which might not be feasible or safe for all users.
- Platform-Specific: The path to the hosts file and the method of modifying it may differ between operating systems, requiring adjustments to the script depending on whether it’s run on Windows, macOS, or Linux.
- Lack of Advanced Features: Unlike specialized website blocking software, this script does not offer scheduled blocking, different user profiles, or detailed reporting.
Conclusion
The Python script for blocking websites is a practical tool for managing online content access directly from one’s computer. While it offers significant benefits in terms of simplicity and control, potential users should consider its limitations and ensure it meets their specific needs.
This tool is ideal for those seeking a straightforward, customizable solution without the need for complex or costly software.

Ishaan is a passionate programmer with a knack for simplifying complex ideas. With expertise in Python and PHP, he enjoys creating small projects that help others learn and apply coding skills practically. Through his articles, Ishaan aims to inspire and educate budding developers.