Site icon DataFlair

How to build Website Blocker in Python

python website blocker project

Python course 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 *

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

3. Host Path and IP Address:

host_path ='C:\Windows\System32\drivers\etc\hosts'
ip_address = '127.0.0.1'

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)

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)

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

7. Main Command:

window.mainloop()

Python Website Blocker Output:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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.

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.

Exit mobile version