Create a website blocker with Python

The outburst of social networking websites is one of the main technological advancements made in 21st century. Social networking is now a part of lifestyle. There is a debate whether the advantages of the social networking outweigh its disadvantages or not but nevertheless social media is now a part of our lives. For those who work with an available internet connection, social media websites can be a distraction eventually affecting the productivity. How can we keep ourselves from accessing these websites?

social-media-websites.jpg

Well there are applications out there to block the websites that you don’t want to be accessed from your PC or PCs if you have a business with employees. But why use third party application if you can create one yourself. This is one of the perks of being a programmer. Design your own custom application. That’s exactly what we are going to do in this tutorial today. Creating your own application gives you flexibility and control over the final product and in the end you can create exactly what suits your needs. The language that we will use is one of the most powerful, most popular and fastest growing languages of the era. Yeah, it’s Python. So let’s dive into it.

Requirements

  • Python 3 installed on your computer
  • Basic knowledge of Python

What we are going to create

We are going to create a Python application that runs in the background and blocks a list of websites during 9 to 5 work hours. We will do this with Python’s built-in libraries, so no need to install external packages.

So how do you block a website? Every operating system has a hosts file. This hosts file is used to map hostnames (or domains) to IP addresses. This host file is used to restrict access to unwanted websites and domains. Most of the website blockers and parental control apps use this host file for this purpose. Following is an image of the host file opened in notepad.

Skip the hashed out text (comments) and scroll down to the bottom where you can see:

127.0.0.1 www.facebook.com
127.0.0.1 www.twitter.com

What these lines of code do is when someone requests access to www.facebook.com or www.twitter.com from a web browser on this PC, they will be redirected back to the local IP address of this PC which is 127.0.0.1. Our application will use file manipulation methods of Python to open this file and put these lines of code in this file.

So why use Python to put these two lines of code in this file when you can do it using notepad? Actually our program will do much more than this. Once our program is running, it will always run in the background and automatically enter these lines of code when the clock ticks 9 AM. Then it will check the clock every 2 minutes and remove these lines of code from the host file when the time is 5 PM. We will use the Python’s built-in libraries date and datetime for this program. This is a very handy solution because you don’t want to log into all computers in your office and do this manually twice every day. Okay enough chat, let start coding this thing. We start by importing the required libraries:

import date
from datetime import datetime as dt

And then we populate a Python list object with all the websites that want to get rid of:

website_list=["www.facebook.com","facebook.com","www.twitter.com","twitter.com"]

Of course you can add as many websites as you can. In Windows operating system, the host file that we want to edit is stored at C:\Windows\System32\Drivers\etc\hosts. We will store this path in a Python raw string. Raw strings are used to store strings with a bunch of special characters. This eliminates the need to escape each special character present in the file path. Raw strings return exactly what you see.

hosts_file = r”C:\Windows\System32\Drivers\etc\hosts”

And then the local IP address of your PC

redirect = "127.0.0.1"

To check the time every 2 minutes we use an infinite while loop that executes every 120 seconds:

while True:
time.sleep(120)

This loop will always run because we have passed the boolean True to this loop. The sleep function will hold your program for 120 seconds every time the while loop is executed. This is a wise choice because you don’t want your program to run every millisecond and burden your system unnecessarily. Now there are two scenarios. The time will be either between 9 and 5 or it will be outside 9 and 5. For each of these conditions we execute a different set of instructions. So we use an if-else statement.

First scenario: Work hours

First part of this if-else will fetch the current time from operating system using datetime.now() method and check whether it is work hours or not.

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):

If the time is between work hours then we open the host file in r+ mode so that we can read and write into it.

with open(hosts_file,'r+') as file:
    content=file.read()

Now we will iterate over the websites list and check whether the websites are present in our host file. If they are then we do nothing because that’s what we want. If not then we write the required lines of text that we want in the host file using file.write() method.

for website in website_list:
    if website in content:
    #Do nothing
        pass
    else:
        file.write(redirect+" "+ website+"\n")

Second scenario: After work hours

Work hours are over now. And who doesn’t like using social media? So now we have to produce a Python code that removes the list of blocked social media websites from the hosts file so that we can enjoy using our favorite social media websites. Now comes the tricky part. We want to remove some lines from the text but there is no method available that deletes part of text from a file. The solution for this scenario is to read the complete file line by line using file.readlines() method and then remove the lines that we want to remove from the content of the file. After this we write the content again in the file. Now let’s open the file in r+ mode and check whether the list of websites is present in the text file or not.

else:
    with open(hosts_path,'r+') as file:
        content=file.readlines()

Now we have loaded the complete content of the file line by line in a list object called content. Let’s go to the start of the file using file.seek() method and check whether the list of websites is present in the content or not. If not then we append the line into file using file.write() method. In this way we are able to filter out the list of websites from the file.

            file.seek(0)
            for line in content:
                if not any(website in line for website in website_list):
                    file.write(line)

And then we truncate the file to avoid for loop entering multiple blocks of text in the file.

            file.truncate()

And with this we complete the Python code that we needed to build this program. This program will run only with administrator privileges because hosts file is a system file and without administrator privileges you can’t edit the hosts file. This is good because employees normally don’t use office computers with administrator privileges and will not be able to edit this. Let’s run the script now using the command line (as administrator). Go to cmd, go to the directory that contains our program and run the Python script. Our script is running now. To verify that let’s try to access Facebook from our browser.

Great! So our script is complete now and working as expected. Running the program as a background process Now let’s run this program as a background process. For this we have to change the extension of our Python script file from ‘.py’ to ‘.pyw’ and we have to run it (as administrator). The ‘.pyw’ are executed with ‘Pythonw.exe’ instead of ‘Python.exe’. Pythonw.exe is present in same folder where Python.exe is present. So let’s run it now. Our program is now running in the background. You can verify this by checking in the task manager.

Now you can put this program into startup so the program will run automatically every time you start your PC. This can be done with the help of windows task scheduler.

So that’s about it. We have now a fully functional Python website blocker running in the background blocking access to unwanted websites in work hours. I hope you enjoyed this tutorial and learned a bunch of new aspects of Python. Happy programming! See you later.

Source code:

import time
from datetime import datetime as dt
hosts_file=r"C:\Windows\System32\Drivers\etc\hosts"
local="127.0.0.1"
website_list=["www.facebook.com","facebook.com","www.twitter.com","twitter.com"]
while True:
    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):
        with open(hosts_file,'r+') as file:
            content=file.read()
            for website in website_list:
                if website in content:
                    pass
                else:
                    file.write(local+" "+ website+"\n")
    else:
        with open(hosts_file,'r+') as file:
            content=file.readlines()
            file.seek(0)
            for line in content:
                if not any(website in line for website in website_list):
                    file.write(line)
            file.truncate()
    time.sleep(120)

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Bitcoin Gambling

Delete an article

Deleted articles are gone forever. Are you sure?