Build a Website Blocker with Python

02 May 2023 Balmiki Mandal 0 Python

How to Build a Website Blocker in Python

If you’re looking to block access to certain websites, the best way is to use a website blocker written in Python. Python is a versatile programming language that can be used to create a wide range of applications. In this guide, we’ll show you how to use Python to block access to specific websites.

Step 1: Setting Up Your Environment

The first step is to set up your environment by installing the necessary software and libraries. You will need a code or text editor (such as Notepad++), along with Python and its related libraries such as pip and virtualenv. Once you have these installed, you’re ready to begin.

Step 2: Writing the Code

Once you have your environment set up, you can begin writing the code for your website blocker. The code will use Python’s ‘hosts’ file to block access to specific websites. The hosts file is a list of website addresses and their associated IP addresses. By adding a website address and IP address combination to the hosts file, you can effectively block access to that website.

Step 3: Testing and Deployment

After you have written the code, you can test it on your machine before deploying it. To do this, run the code and check that the websites you wanted to block are not accessible. If they are, you can tweak the code until the websites are successfully blocked. When you’re happy with the results, you can deploy the code using the Python library for deployment.

Step 4: Maintaining the Code

Once your website blocker is up and running, it’s important to keep the code updated. As new websites are created, you will need to update your code so that those websites are blocked. Additionally, if you want to add new websites to the blocklist, you will also need to update the code.

Python code that blocks access to specific websites on your computer:

import time
from datetime import datetime as dt

hosts_path = r"C:\Windows\System32\drivers\etc\hosts"  # Path to the hosts file
redirect = "127.0.0.1"  # IP address to redirect to
website_list = ["www.facebook.com", "www.youtube.com"]  # List of websites to block

while True:
    # Check if the current time is within the working hours
    if dt(dt.now().year, dt.now().month, dt.now().day, 9) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day, 17):
        # Open the hosts file and read its contents
        with open(hosts_path, 'r+') as file:
            content = file.read()
            for website in website_list:
                if website in content:
                    pass
                else:
                    # Add the website to the hosts file
                    file.write(redirect + " " + website + "\n")
    else:
        # Open the hosts file and read its contents
        with open(hosts_path, 'r+') as file:
            content = file.readlines()
            file.seek(0)
            for line in content:
                # Remove the blocked websites from the hosts file
                if not any(website in line for website in website_list):
                    file.write(line)
            file.truncate()
    time.sleep(60)

In the above code, we first import the required libraries - time and datetime. We then define the path to the hosts file and the IP address to redirect to. We also specify the list of websites to block.

We then create a while loop that runs continuously. Within the loop, we check if the current time is within the working hours (in this example, 9 AM to 5 PM). If it is, we open the hosts file and add the IP address and website URLs for the websites to be blocked. If not, we open the hosts file and remove the blocked websites from it.

We use the time.sleep() method to pause the loop for 60 seconds between each iteration.

 

Note that this code needs to be run with administrator privileges on Windows computers in order to modify the hosts file. Also, the path to the hosts file may be different on different operating systems.

Conclusion

Creating a website blocker in Python is a relatively straightforward process. With a few simple lines of code, you can have a working website blocker in no time. Be sure to test the code thoroughly before deploying it, and regularly update the code to ensure that all the websites you wish to block are indeed blocked.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.