You are on page 1of 7

WEBSITE BLOCKER USING PYTHON

SUBMITTED BY:

NAME OF THE STUDENT ROLL NUMBER


D.CHANDRIKA 21075A0401

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING

VALLURUPALLY NAGESWARA RAO VIGNANA JYOTHI INSTITUTE OF


ENGINEERING AND TECHNOLOGY

AN AUTONOMOUS INSTITUTE

(Approved to AICTE-New Delhi, Govt. of TS and affiliated to JNTUH)


Accredited by NBA and NAAC with A++ Grade Vignana Jyothi Nagar,
Bachupally, Nizampet (S.O),Hyderabad-500090, Telangana, INDIA.
CONTENTS:

 Introduction

 Technology Stack

 Working
 Python code
 Uses and
advantages
 Conclusion
INTRODUCTION:

In an IT company you may notice that a couple of websites are


blocked especially social networking sites like Facebook, YouTube, Instagram
etc. Instead of using third-party applications to blocks certain website, we can
develop our own custom application which will block websites of our choice
and developing a website blocker in python is not so difficult too. That’s what
we going to develop a python script which will block the website we want.
Prerequisites to block a website are Python 3.x installed & basic knowledge of
Python. We are going to develop python application which will block a certain
website (whatever website you want- Facebook, YouTube etc.) during certain
working hours, consider office hours of the day, we want to blocks all social
networking sites. What we are going to in this program is that we will pass the
link of websites which you think are distracting and the time that you are
working on your computer and program will block those websites. We are
going to use the python built-in libraries, so no need to install any third-party
packages. Every operating system has a hosts file. Location of the host file may
be different for the different operating system. This host file maps hostname to
IP address of the machine. In this host file, we going to list websites which we
want to block.

TECHNOLOGY STACK:
1. Visual Studio Code
2. Task Scheduler
3. Python

WORKING:
Utilizing Python as the Programming language i.e. an elevated level, broadly
useful programming and exceptionally intruded on language. Every
operating system has a hosts file. The location of the host file may be
different for the different operating systems. This host file maps hostname to
the IP address of the machine. The host document would have been fed by
some fundamental information listing the websites needed to block. To build
a python-based project, which will be assisting the user with blocking a
specific website, like Instagram, Facebook, YouTube, etc. and so on for a
specific time of the time like 9:00 to 10:00 hours. We use Visual Studio
Code and Task Scheduler (specially for windows) to run the application.
 Our first step is to locate the Host file.
 Open Notepad as administrator and open that Host file.
 List the websites that we want to block along with the IP address.
 Open Visual Studio Code and start writing the program.
 Import time and import date time as dt. Add the Host path and IP
address.
 List the websites need to be blocked.
 Give a while loop statement, the whole program runs in this while
loop.
 While true, If the present datetime that we give is greater than the
number(hour) say 8 then it prints working hours, else it prints fun
time.
 Then open the Task scheduler and create a task to run this program.
When we run the task, the websites are being blocked and when we
end the task the websites are unblocked.

Block diagram:

FLOW CHART:
PYTHON CODE:
import time
from datetime import datetime as dt
hosts_path = (r“C:\Windows\System32\drivers\etc\hosts”) # r is for raw
string.
hosts_temp = “hosts”
redirect = “127.0.0.1”
websites_list = [“www.facebook.com”, “facebook.com”] # users can modify
the list of the websites they want to block.

while True:
    if dt(dt.now().year, dt.now().month, dt.now().day, 8) < dt.now() <
dt(dt.now().year, dt.now().month, dt.now().day, 23):
        print(“Working hours”)
        with open(hosts_path, “r+”) as file:
            content = file.read()
            for website in websites_list:
                if website in content:
                    pass
                else:
                    file.write(redirect+” “+website+”\n”)
else:
        print(“Fun time”)
        with open(hosts_path, “r+”) as file:
            content = file.readlines()
            file.seek(0)  # reset the pointer to the top of the text file.
            for line in content:
                # here comes the tricky line, basically we overwrite the whole file.
                if not any(website in line for website in websites_list):
                    file.write(line)
                # do nothing otherwise.
            file.truncate() # this line is used to delete the trailing lines (that
contain DNS).
    time.sleep(5)
USES AND ADVANTAGES:

 This application can be used to block the websites so that the user can not
open them during the specific period.
 In this, we will block the access to the list of some particular websites
during the working hours so that the user can only access those websites
during the free time only.
 The primary benefit of solutions for blocking web access is that they can
prevent Internet users visiting websites harbouring malware or
ransomware, websites that are known to be fake and used for phishing
attacks, and websites that hide their true identity behind a proxy server or
the which is privacy feature.

CONCLUSION:
The project entitled “WEBSITE BLOCKER” is developed using
Python to block the websites which seems distracting during the working hours.
We are all aware that while surfing the internet, a slew of undesirable websites
shows up to divert our attention. This project comes in handy in such situations
since it can be set up to prevent particular websites from loading. So, one can
build their own website blocker application using Python which is an easy
coding language.

You might also like