HOW TO BLOCK WEBSITES USING PYTHON


HOW  TO  BLOCK  WEBSITES  USING  PYTHON:


Hello Everyone, welcome to Techienoobs in this blog we will get to know how to block a website using python. The methods which are shown now are just for educational purposes we are not responsible for any kind of  misuse of the information in this blog.

                           Few people in work places like offices, colleges, internet cafes visit websites which they are not supposed to use. Many people block the websites by using the default browsers settings so that the intended persons do not use those websites which have been blocked. But do you know you can do the same with python with a simple code along with a GUI(Graphical User Interface). Yes, In this blog I am going to show you how to make your own website blocker with python along with the complete source code. Follow the below steps to make your own website blocker.

Note: This works only on windows

STEP 1:  Open your command prompt.

STEP 2:  Type the following command

                            pip install tk

STEP 3:  After the packages are installed open idle 

STEP 4: Copy the following code and paste it in your ide.


CODE:

#import library
from tkinter import *

#initialize window
root = Tk()
root.geometry('500x300')#size of the window
root.resizable(0,0)
root.title("my blocker")#window name

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

#path of our host file ang ip adsdress 
host_path = 'C:\Windows\System32\drivers\etc\hosts'#system path to block websites
ip_address = '127.0.0.1'

#ENTER WEBSITE
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)

#block function using file concept
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)

block_btn = Button(root, text = 'BLOCK' , font = 'arial 12 bold', command = Blocker, width = 6 , bg = 'royal blue1', activebackground = 'sky blue')
block_btn.place(x = 230, y =150)

root.mainloop()
    



    you might get some syntax errors here but you can easily remove them by adding or removing spaces. If you don't have any errors then save it and close it.
 

STEP 5: Now open that saved file with administrator permissions Enter few domains of the websites and click on block button and the websites have been blocked.


Now that we have blocked the websites successfully let us now unblock the site which have been blocked .follow these steps to unblock the websites.

STEP 1: Open your Notepad with administrator permissions and navigate to C:\Windows\System32\drivers\etc\  Now open the hosts file  in notepad.

STEP 2: There you will see your website names and remove them along with the ip address 127.0.0.1  and save it your websites will be unblocked.

here is the video of how to do it:



Comments