Signup/Sign In
PUBLISHED ON: MARCH 7, 2022

Python Project for Website Blocker

Python Project for Website Blocker

Website Blocker is a program that blocks access to websites either permanently or on a regular basis. We can ban all websites from undesirable categories to use the internet securely.

Python Project to Block Websites

The goal of the Website Blocker python project is to ban certain websites from being accessed from any device. This project will assist the user in avoiding distractions by preventing them from accessing web pages on their smartphone.

The user may input numerous websites to block in this Python Website Blocker Project, and then hitting the block button will verify the condition that if the website has already been banned, it will print 'already blocked', else it will block all of the websites and print 'blocked'.

Prerequisites for the project

The fundamental ideas of Python and the Tkinter library will be used to construct the website blocker project.

Tkinter is a well-known Python GUI library. It is one of the quickest and most straightforward methods to create GUI applications using Tkinter.

You may use the pip install command at the command line to install the library:

pip install tkinter

How to Make a Website Blocker Project Python:

  • The module is being imported.
  • Make a display window.
  • Make a widget for entry.
  • Create a function.
  • Make a button that says "block."

1. Importing the module is the first step.

from tkinter import *

We use the tkinter library to import modules.

2. Create the display window

root = Tk()
root.geometry('500x300')
root.resizable(0,0)
root.title("TechVidvan - Website Blocker")

We'll use the tkinter library to construct a window where we'll type our text to be converted into voice.

Tk() created tkinter, which signifies that a window was formed.
geometry() makes the window's width and height resizable (0,0) set the window's fixed dimensions
bg = " is used to set the window's background color, whereas title() is used to set the window's title.

Label(root, text ='WEBSITE BLOCKER' , font ='arial 20 bold').pack()
Label(root, text ='TechVidvan' , font ='arial 20 bold').pack(side=BOTTOM)


The Label() widget is used to show one or more lines of text that users cannot change.

root – the name of our window text – the text that appears on the label font – the typeface in which the text is written pack – the organized widget in block 3 Make a widget for entry.

  • The path to our host file is saved in host path.
  • The IP address used by localhost is saved in ip address.
  • For multi-line text fields, the Text() widget is utilized.
  • After the final word, wrap = WORD will break the line.
  • Padx adds more room to the widget's left and right sides, while Pady adds extra space to the widget's top and bottom sides.

4. Create a function.

host_path ='C:\Windows\System32\drivers\etc\hosts'
ip_address = '127.0.0.1'
Label(root, text ='Enter Website :' , font ='arial 13 bold').place(x=5 ,y=60)
Websites = Text(root,font = 'arial 10',height='2', width = '40', wrap = WORD, padx=5, pady=5)
Websites.place(x= 140,y = 60)

website lists website list(lists.split(,"")) returns a list of all the websites that users may input. comma-separate the content of the lists, then convert it to a list and save it in a website with open – When we are done using the file, the with open statement will automatically terminate the file handler.
r+ will be used to read and write to a file.
Print a label with text previously blocked if the webpage is already in file content.
Otherwise, it will ban all of the specified websites and display a label that reads 'Blocked.'

5. Make a button that says "Block."

def Blocker():
    website_lists = Websites.get(1.0,END)
    Website = list(website_lists.split(","))
    with open (host_path , 'r+') as host_file:
        file_content = host_file.read()
        for website in Website:
            if website in file_content:
                Label(root, text = 'Already Blocked' , font = 'arial 12 bold').place(x=200,y=200)
                pass
            else:
                host_file.write(ip_address + " " + website + '\n')
                Label(root, text = "Blocked", font = 'arial 12 bold').place(x=230,y =200)

When we press the Block button, the Blocker function is invoked.

When we click the button, Button() is called, which is utilized to show the button on our window command.
activebackground – specifies the color of the button's background when it is pressed.

Source Code for Website Blocker in Python Language

from tkinter import *
root = Tk()
root.geometry('500x300')
root.resizable(0,0)
root.title("TechVidvan - Website Blocker")
Label(root, text ='WEBSITE BLOCKER' , font ='arial 20 bold').pack()
Label(root, text ='TechVidvan' , font ='arial 20 bold').pack(side=BOTTOM)
host_path ='C:\Windows\System32\drivers\etc\hosts'
ip_address = '127.0.0.1'
Label(root, text ='Enter Website :' , font ='arial 13 bold').place(x=5 ,y=60)
Websites = Text(root,font = 'arial 10',height='2', width = '40', wrap = WORD, padx=5, pady=5)
Websites.place(x= 140,y = 60)
def Blocker():
    website_lists = Websites.get(1.0,END)
    Website = list(website_lists.split(","))
    with open (host_path , 'r+') as host_file:
        file_content = host_file.read()
        for website in Website:
            if website in file_content:
                Label(root, text = 'Already Blocked' , font = 'arial 12 bold').place(x=200,y=200)
                pass
            else:
                host_file.write(ip_address + " " + website + '\n')
                Label(root, text = "Blocked", font = 'arial 12 bold').place(x=230,y =200)

Output

Block

Summary

The Website Blocker python project has been completed successfully. For producing graphics on a display window, we utilized the famous Tkinter package and the fundamentals of Python programming.



About the author:
Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.