Website Blocker using Python

Website Blocker using Python

It is common that you will be distracted by Social media when you go on to work or study. This lets you increase procrastination and decrease productivity.

How can I focus and get rid of this?

Here's a cool hack with Python.

Do you Know? You can Block the websites using Python.


This is a beginner's project in Python. There is the hosts' file in every Operating System.

Hosts: It is an operating system file that maps hostnames to IP Addresses. It consists of Ip Address followed by one or more hostnames. It is available in different paths depends upon Operating System.

Using the concept of files, we can edit the Hosts file (hosts.txt) and add the websites that you want to Block.

In this program, we are going with the Windows Operating System. In windows hosts.txt path is

C:\Windows\System32\drivers\etc\hosts

Here's I keeping the things simple. First look at the Program.

# Run this script as Administrator
import time
from datetime import datetime as d
# Hosts path of Windows. change hosts path according to your OS
hosts_path = "C:\Windows\System32\drivers\etc\hosts"
# localhost's IP
redirect = "127.0.0.1"
# add website to websites list to block
websites =["www.facebook.com","facebook.com","www.gmail.com","gmail.com"]
while True:
# time of your work
if d(d.now().year, d.now().month, d.now().day,8) < d.now() < d(d.now().year, d.now().month, d.now().day,16):
print("Working hours")
with open(hosts_path, 'r+') as file:
content = file.read()
for website in websites:
if website in content:
pass
else:
# mapping hostnames to your localhost IP address
file.write(redirect + " " + website + "\n")
else:
with open(hosts_path, 'r+') as file:
content=file.readlines()
file.seek(0)
for line in content:
if not any(website in line for website in websites):
file.write(line)
# removing hostnmes from host file
file.truncate()
print("Fun hours")
time.sleep(4)

Clear Explanation:

  1. Provide the path of your hosts.txt file according to Operating system
  2. Add the websites that you want to block to the list. So, we can iterate through each element in the list and block each website.
  3. Running a loop, and define the time for hours to block the website. For example, I want to block all websites in my Study time between 10:00 AM to 12:00 PM.
  4. Then in if loop you need to define d(d.now().year, d.now().month, d.now().day,10) < d.now() < d(d.now().year, d.now().month, d.now().day,12)
  5. Opening the hosts.txt file with open(hosts_path, 'r+') as file and add content to list.
  6. If it is between 10:00AM and 12:00PM block the websites else truncate all added websites to list.
Steps to run the Program:

Open your IDE or Notepad using Administrator because you need permission to access files in C Drive.
Hosts File 



Typing all the Code



Running



Try to open and enjoy your Working Hours.



Image of hosts.txt file while running your Program.



You can see all the websites are added to hosts.txt file

I will create all the Python beginner's projects in this Blog. Many of the first and second-year students don't know the importance of Projects. I am not a pro in Coding to tell this but my intention do projects whether it is small or big, it helps to improve your Coding Skills.

You may ask? What's new in this Project its a simple program?

I created my version of the project that I created a clean user interface for this project. It asks the websites that you want to Block and also the number of minutes or hours to block.
I will update the project in another blog and clean explanation about Tkinter.
//Img

Ex: If you want to block the website for 45 min. you can.

So create your own version of Projects and play with them. Follow this blog for more interesting articles

Comment about any queries and any other interesting projects.

If you want to get notified about TCS OPA solutions and coding Subscribe to this Blog.

Have a nice day😀.



Post a Comment

Previous Post Next Post