How to build Website Blocker in Python

Free Python courses with 57 real-time projects - Learn Python

Instead of using other applications to block websites on your system, why not create our own website blocker application using Python? Let us master Python by making yet another mini-project.

About Website Blocker

There are certain times when we face the need to block different websites on our system. This is where a Website Blocker helps us.

We are going to create a website blocker using python which will give us a window where we can type the url of the websites and then we can block those websites. We will also have an unblock feature added to it so that we can unblock any website in future if we want and Tkinter Module for creating GUI.

Project Details

While creating Python Website Blocker Application, we are going to use only one module – Tkinter Module to make easy GUI using Python. We will be making two functions Block() and Unblock(). As the names suggest these functions will perform blocking and unblocking of websites respectively. These websites are added to the host file of the system.

Python Website Blocker Project Prerequisites

This project requires basic understanding of Python like defining functions, creating loops etc. To use Tkinter Module we need to install it using the following command:

pip install tk

Download the Source Code

Before proceeding with the project, please download the source code of python website blocker from the following link: Website Blocker Project

Project File Structure

Let us have a look at the steps we will be performing to create the Python Website Blocker project

  1. Importing the required library
  2. Creating GUI window
  3. Hostpath and IP address.
  4. Creating Block function to block websites
  5. Creating Unblock function to unblock websites
  6. Creating labels and buttons
  7. Main command

Let us start building the python project following the given steps

1. Importing the required library:

We will only require Tkinter Module to create GUI in Python.

#importing required library
from tkinter import *
  • Import * means importing all the methods and functions from the tkinter library.

2. Creating GUI Window:

window = Tk()
window.geometry('650x400')
window.minsize(650,400)
window.maxsize(650,400)
window.title(" DataFlair Website Blocker")

heading=Label(window, text ='Website Blocker' , font ='arial')
heading.pack()
  • tk()- helps us create an empty window where we can add labels and buttons. We have created a space named window.
  • Geometry() – this function is used to give size to the window.
  • minsize(), maxsize() – this function is for giving the minimum and maximum size to the window.
  • title() – provides an appropriate title to the window.
  • Here we have created a Label named heading which is for giving a heading to the window. We have used Label() to create the label and pack() to pack the heading label to the window.

3. Host Path and IP Address:

host_path ='C:\Windows\System32\drivers\etc\hosts'
ip_address = '127.0.0.1'
  • As we want the websites to be blocked in our system we need to add those websites in the host file. So here we give our local ip address and path of the host file.

4. Create Block Function:

def Blocker():
  website_lists = enter_Website.get(1.0,END)
  Website = list(website_lists.split(","))
  with open (host_path , 'r+') as host_file:
   file_content = host_file.read()
   for web in Website:
     if web in file_content:
       display=Label(window, text = 'Already Blocked' , font = 'arial')
       display.place(x=200,y=200)
       pass
   else:
       host_file.write(ip_address + " " + web + '\n')
       Label(window, text = "Blocked", font = 'arial').place(x=230,y =200)
  • We have created this block function so that when we add any website to the text area and click on the block button, the website will be added to the host file and will be blocked. If the website is already in the host file then a label ‘Already Blocked ’ will be displayed.
  • get() – this method is used to get the text that is added to the enter_website label.
  • open() – this is for opening the host file. Here we have opened the host file in r+ mode which is read plus write mode.
  • split() – this method is used to separate the content of the text area. Here we used (,) as our delimiter to separate the website list.
  • After opening the file, with help of a loop we check if the website entered is already present then display label “Already Blocked” and if it is not present add it to the file using write() method and then display label as “Blocked”.

5. Unblock Function:

def Unblock():
    website_lists = enter_Website.get(1.0,END)
    Website = list(website_lists.split(","))
    with open (host_path , 'r+') as host_file:
     file_content = host_file.readlines()
    for web in Website:
      if web in website_lists:
       with open (host_path , 'r+') as f:
        for line in file_content:
         if line.strip(',') != website_lists:
           f.write(line)
           Label(window, text = "UnBlocked", font = 'arial').place(x=350,y =200)
           pass
       else:
           display=Label(window, text = 'Already UnBlocked' , font = 'arial')
           display.place(x=350,y=200)
  • The Unblock() function is created so that we can unblock a website that is already blocked and is present in the host file. If a website is blocked and we click on the Unblock button a label “Unblocked” is displayed. If a website is already Unblocked and isn’t a part of the host file then display label – “Already Unblocked”.
  • get() – this method is used to get the text that is added to the enter_website label.
  • open() – this is for opening the host file. Here we have opened the host file in r+ mode which is read plus write mode.
  • split() – this method is used to separate the content of the text area. Here we used (,) as our delimiter to separate the website list.
  • We read the content of the file and if the entered website is present we rewrite all the content in the file except the one we need to unblock and display the label “Unblocked. And if there is no such website in the host file we simply display the label – “Already Unblocked”.

6. Creating Labels and Buttons:

label1=Label(window, text ='Enter Website :' , font ='arial 13 bold')
label1.place(x=5 ,y=60)

enter_Website = Text(window,font = 'arial',height='2', width = '40')
enter_Website.place(x= 140,y = 60)
  • Here we have created one Label using the Tkinter library. One label is to display text -”Enter Website:” and the text area is to provide a text area to enter websites.
  • The method Label() is used to create a label.
  • For the text area, we use the method Text().
  • To place both of these to the window we use the place() method and inside it we give the values to x and y.
  • We can specify the text we want to display, font,height, width,bg,fg for each of these.
block_button = Button(window, text = 'Block',font = 'arial',pady = 5,command = Blocker ,width = 6, bg = 'royal blue1', activebackground = 'grey')
block_button.place(x = 230, y = 150)

unblock_button = Button(window, text = 'UnBlock',font = 'arial',pady = 5,command = Unblock ,width = 6, bg = 'royal blue1', activebackground = 'grey')
unblock_button.place(x = 350, y = 150)
  • Here we have created 2 buttons one for block and another for unblock. We make use of the Button() method to create these buttons which is an inbuilt method of the Tkinter Module. We make use of the place() method to place these buttons on the window.
  • Command= function_name – This syntax is used inside the Button() method to specify what task a button will do when it is clicked. In this case, block and unblock are the two tasks.
  • We can also specify a number of things like text on the button, font, bg,fg etc.

7. Main Command:

window.mainloop()
  • The mainloop function is used to run the window and display the output on the window.
  • While working with the host file, to run the code we need to open the code as admin in the command prompt and then run to make the changes in the hostfile. If we try to run the file directly it will show an error – “Permission not granted.”

Python Website Blocker Output:

Let’s have a look at what happens when we try to block a website. See a blocked label is visible to us showing that the website has been blocked.

python website blocker output

Summary:

We successfully created a Website Blocker Project using Python. Using this application we can block and unblock any websites that we want. We made our GUI using the Tkinter Module and its inbuilt methods.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

follow dataflair on YouTube

10 Responses

  1. mati says:

    unblocking is not working

  2. Sai krishna says:

    Hello sir/madam, firstly thank you for providing such useful content. I tried a Python project from your website i.e website blocker. Code is executed and I can see the interface of Tkinter but am unable to block the website it shows (an exception in Tkinter callback and also shows a permission error) please solve my query.
    Thank you.

    • Gokul says:

      Hello sir/madam, firstly thank you for providing such useful content. I tried a Python project from your website i.e website blocker. Code is executed and I can see the interface of Tkinter but am unable to block the website it shows (an exception in Tkinter callback and also shows a permission error) please solve my query.
      Thank you.

  3. SURAJ KUMAR says:

    i HAVE IMPLIMENTED THE PROGRAM AND IT RUNS SUCCESSFULLY.
    IT BLOCKED TO A SITE…
    BUT THE PROBLEM ARISE IN UNBLOCKING THE SITE
    COULD YOU PLEASE HELP ME…’

  4. sunil says:

    i HAVE IMPLIMENTED THE PROGRAM AND IT RUNS SUCCESSFULLY.
    IT BLOCKED TO A SITE…
    BUT THE PROBLEM ARISE IN UNBLOCKING THE SITE
    COULD YOU PLEASE HELP ME…

  5. SOMEONE says:

    Where are the buttons? they don’t show up

  6. Gokul says:

    Hello sir/madam, firstly thank you for providing such useful content. I tried a Python project from your website i.e website blocker. Code is executed and I can see the interface of Tkinter but am unable to block the website it shows (an exception in Tkinter callback and also shows a permission error) please solve my query.
    Thank you.

  7. Stepan says:

    How do you even solve the unblocking issue. My google.com is blocked and cannot be unblocked, for the past day, I have tried almost everything. Nothing works. Thank you.

  8. Diego Andrés Fernándes Romero says:

    Unblock FIXED!

    def Unblock():
    website_lists = enter_Website.get(1.0, END)
    Website = list(website_lists.split(“,”))
    with open(host_path, ‘r’) as host_file:
    file_content = host_file.readlines()
    with open(host_path, ‘w’) as f:
    for line in file_content:
    if not any(web in line for web in Website):
    f.write(line)
    display = Label(window, text=’Unblocked Successfully, font=’arial’)
    display.place(x=350, y=200)

Leave a Reply

Your email address will not be published. Required fields are marked *