Creating a Website Blocker Using Python Script

In a world filled with digital distractions, maintaining focus can be a challenge. But fear not, Python comes to the rescue! In this blog, we’ll explore how to create a website blocker using a Python script. This blocker will help you curb online distractions and boost your productivity.


Before we dive into the code, ensure you have:

  • Python Installed: If you haven’t already, download and install Python from the official Python website.
  • Text Editor: You can use any text editor or integrated development environment (IDE) to write and execute your Python script.

Building the Website Blocker

We’ll be using Python to edit your computer’s “hosts” file, a system file used to map domain names to IP addresses. By redirecting unwanted domains to a local address, we effectively block access to those sites.

Here’s a step-by-step guide:

  1. Locate the Hosts File: The “hosts” file is usually located in the following path:
  • Windows: C:\Windows\System32\drivers\etc\hosts
  • Linux/Unix: /etc/hosts
  • macOS: /private/etc/hosts
  1. Open the Hosts File: You’ll need administrator/root privileges to edit the file. You can use a text editor or the terminal to open it.
  2. Add Blocked Sites: At the end of the file, add lines to redirect unwanted sites to the local IP address ( or ::1 for IPv6):
  1. Create a Python Script: Create a Python script that modifies the hosts file:
import time
from datetime import datetime as dt

hosts_path = "/etc/hosts"  # Change this path based on your OS
redirect_ip = ""
blocked_sites = ["", ""]  # Add more sites

while True:
    # Define working hours (24-hour format)
    start_time = dt(,,, 8)  # 8 AM
    end_time = dt(,,, 16)   # 4 PM

    if start_time <= <= end_time:
        with open(hosts_path, "r+") as file:
            content =
            for site in blocked_sites:
                if site in content:
                    file.write(redirect_ip + " " + site + "\n")
        print("Websites blocked during working hours.")
        with open(hosts_path, "r+") as file:
            content = file.readlines()
            for line in content:
                if not any(site in line for site in blocked_sites):
        print("Websites unblocked outside working hours.")

    time.sleep(5)  # Check every 5 seconds
  1. Run the Script: Open your terminal, navigate to the directory where the script is saved, and run it using sudo (administrator/root privileges):
sudo python

This script runs an infinite loop that blocks the specified sites during defined working hours and unblocks them outside thoose hours.


Creating a website blocker using a Python script is a powerful way to regain control over your online activities. By redirecting distracting sites to a local address during your work hours, you can stay focused and boost your productivity.

Remember that this method is a simple form of blocking and can be bypassed by advanced users. For a more comprehensive solution, consider using third-party tools or built-in parental control features provided by your operating system.

With this guide, you can create your own cuustomized website blocker and tailor it to your specific needs. Happy coding and focused working!

Blogs You Might Like: