Vsshyam Report

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

CHAPTER 1

INTRODUCTION

1.1 Introduction:

In today’s world, Social media is an important way to stay in touch with friends or connect
with new ones, but there is a down side too. Spending too much time focusing on social media
can cause a lot of powerful negative emotions like stress, anxiety, distractions etc.

Struggling with productivity issues can feel like a very personal problem. But if you think
you’re alone, think again. Distraction and procrastination have become the norm!

For example, go take a look at the constant noise on your social feeds. People post something
new every few minutes. And constant barrage of tweets on Twitter. Stories shared across Snap
chat. But this constant state of distraction and playing around online when you should be busy
is not something most people can control.

1.2 Objectives:

● To develop and build a python-based project, which will assist the user with blocking a
specific site, like Instagram, Facebook, YouTube, etc for a specific time.
● To block certain websites which can distract a person during the specified amount of
time and block the access of particular websites during the working hours so user can
access those only during fun hours.

1.3 Motivation

In fact, one study by Michigan State University back in 2013 showed that even the smallest
distractions, like being interrupted by a quick message on a Face book or shopping app, caused
twice as many errors in focus-based tasks. Even worse, most students and employees tend to
drastically overestimate their ability to continue performing as usual while these distractions
go on. And with the buzz of today’s technologies constantly in the background of our entire
day, one can only guess what this cost in performance across every realm.
According to a survey conducted by Pew Research in 2018, Facebook and YouTube
dominated the social media landscape, as notable majorities of adults used each of these sites.

1 | Page
In the war against internet procrastinations people are trying to focus and prevent distractions
while working hours on the internet to block access to websites which can distract a person.
This is website blocking. This is a real world program which blocks distracting websites during
your work hours.

1.4 Overview:

In this program we will pass the link of websites which we think is distracting and the time
that we are working on our computer and program will block those websites. Many of us
struggle to focus nowadays, easily distracted by Social media and some sites on the internet
which dramatically affect our productivity. In this, we will learn to create website blocker using
python to block certain selected sites that distract us during working hours. The working time
in this python is considered from 9 AM to 5 PM. The time period except that time will be
considered as free time.

For programming reason, we are going to utilize Python. Python is easy to learn and work
on with the language. It is an elevated level, broadly useful programming and exceptionally
intruded on language.

We are going to use time and Date time Module only which comes by default with the
Python Standard Library.

How do we block sites?

Every operating system has a hosts file and it’s here where we are going to add a list of
websites we want to block. We will add website URLs which are mapped to local hosts thus
preventing us from accessing the real site, for instance: Some fundamental information on the
host document since we are going to list sites which we need to block. Every operating system
has a hosts file. The location of the host file may be different for the different operating systems.
This host file is a map hostname to the IP address of the machine.

2 | Page
CHAPTER 2
IMPLEMENTATION

2.1 Operating System – Host File :

Every operating system has a hosts file. Location of the host file may be different for the
different operating systems. This host file is a map hostname to the IP address of the machine.
In this host file, we are going to list websites which we want to block.

The location of the hosts file varies from operating system to operating system:

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

Steps to open Hosts file:


Step1: In windows open C drive

3 | Page
Step 2: In C drive select Windows

4 | Page
Step 3: Then click on System 32

5 | Page
Step 4: In system 32 open drivers

Step 5: Click on drivers

6 | Page
Step 6: Then open hosts using Notepad

7 | Page
Every host file looks like this:

8 | Page
2.2 Steps to build Website blocker using Python:

Step1:
Open Atom IDE to code

Atom is free and open source. It is a desktop application which is designed to serve Python
developers.
Step 2:
Create a python file as websiteblocker and giving extension as .py

I copied hosts path file and keep that path in a variable say host path and I passed r prefix which
means a row which tells python that you’re actually passing a row string but not passing any
break lines. redirect=”127.0.0.1” means IP web browser visit will be redirected when you
browse Facebook or YouTube.

9 | Page
Now, we need to pass the websites that you want to block by creating a variable website_list.
“www.facebook.com” and “facebook.com” because sometimes websites don’t use their
domains to open i.e., www

Now, we don’t want to open these websites during specified period of time let's say 9am to 5pm
and from 5pm they should open and it should run all the time.
Step 3:
Using a While loop, while loop executes an action very fast.

Using Conditional statement IF loop checks the time every millisecond between 9am to 5pm
but it is not correct so we are importing a time module and type time.sleep(); which checks
every second instead of millisecond.
Import datetime module from datetime object as dt

Now we need to check the current time against 9am to 5pm. But current time would be like this
if we check in command prompt (cmd).Current time is generated automatically.

Given below example checks whether current time is less than or not and prints False as current
time is earlier than given.

10 | Page
And if we check current time greater than or not it prints true.

In python script, instead of typing dt(2021) we have python generate year as dt.now().year
which returns as 2021

We want to have fixed time from 9 to check if current time i.e., dt.now() greater than 9.I f it is
greater than 9 means we are in the working hours and we want this to be less than 17

Now we need to access the hosts file by using file handling methods and modify it depending
on time. But this host file can be modified by the administrator which is if I run the python
program I have to make a python editor as administrator because with a simple console I cannot
access the host file.
So, I created a hosts_path variable and set its path

Now if the content in the websites_list is not present in the hosts file then we need to add those
in hosts file.

11 | Page
Step 4:
if we are in between 9am to 5pm then we have to make data in website_list which corresponds
each list item redirects IP but without ‘#’Hash tag.

Open host_path using a ribbon method which we could read the file content but we wouldn’t
be able to write within this with statement so I’m going to pass ‘r+’ now we are able to read
and append the text at the same time.

Now we are going to open the hosts file using the open method and we need to check the status
of the host file without content in website_list and with content.

12 | Page
To check the content, we refer to the host file in python by applying a loop which iterates list
and, on each iteration, apply a condition if this item is in content then continue where the
content is read from host file if item is not in content then add that item in host file.

We have content and we need to iterate through website_list. So, in website_list website is in
host file content then we are in working hours which is part of if conditional.
Ex. If facebook.com is there already then, we don’t do anything because that will already block
that Facebook website
So we pass ‘pass path’ which means that python will go ahead and check for other lines.
Step 5:
Suppose if condition is not executed then python goes to else condition and sleeps for 5 seconds
it starts over again the condition is being evaluated again working hours is printed if we are in
between 9am to 5pm then we open file in second iteration of while loop and we read file, iterate
and checks if any of our list items are in host file and we pass to next lines of code if website
is not there.

But ‘If’ part there then we apply else condition

To make run our website one should use redirect IP by creating redirect variable then according
to host file style we should have a space as its said in below figure

13 | Page
file.write(redirect+" "+website+"\n")
So, we are going to separate with one space in double quotes as “ “ and +website to avoid text
being passed in one single line so we should pass here backslash n with special character(\n)
that will create break lines between these text items.
If the first part is false then we have to execute .
There is no direct method to pass line or arguments as input and then have a delete function that
removes those lines from a file that makes a situation a bit tricky.so our input in the host file is
website_list content which means logically we need to read this file and then check if this
content is present in host file.
So, let me pass else condition

Now let’s store the text in host file in a variable called content as a single string
Content=file.read(); probably this is not a best solution.
so, the best solution would be to use read lines instead which produce a list with all the lines in
our host file.

14 | Page
So content is now all lists we have at least we can start checking these items against the list that
we have grabbed from hosts file so if these items are there we want to delete them but as said
there is no direct way to deal with these items and we have methods to write content ,append
the content in that file but not to delete however we can make use of append method we could
start iterating through the items in content list and check
Example: If Facebook is in that particular item string then we ignore that line and if Facebook
is not there we write that line in the host file.
Step 6:
Using for loop iterates the lists in content and to checks if content in website_list is not in
current line of content list then we write in host file

Here I want to check these items against the lines of content by using ‘if loop’ this checks the
first item in content and it will compare with the hostfile continually until this list is consumed.

Truncate method: The truncate () method resizes the file to the given number of bytes. If the
size is not specified, the current position will be used.
When we apply read methods to file reads the lists in hostfile and seek method tells python to
put pointer before the first statement

15 | Page
Now if we run this python script,

I got a permission error because we are owning host_file as a normal user instead run host_file
as administrator.

2.3 Making hosts file as admin:


Step1: Create a shortcut, type location as location of notepad.

16 | Page
Step 2: Save as Notepad-Admin and right-click select on properties. A dialog box appears click
on Advanced.

Step 3: Click to run as administrator then press ok

17 | Page
Step 4: Now open Notepad-Admin and click all files instead of .txt.

Step 5: Click on hosts to open it

18 | Page
Step 6: Hosts file looks like this and I have written the domain sites which I want to block
already.

Now if we run python script i.e., website blocker its running and printing as working hours.

If I try to open YouTube now

19 | Page
2.4 Implementing the program to run in the background on Windows:

As soon as the computer starts i.e., the python script we build will run as a process in the
background.
Step1: Change the extension .py to. pyw

To Implement Scheduling as soon as computer starts Step


Step2: Open Task Manager.

Step 3: Click on “create task”. Fill the name as websiteblocker and flag “Run with highest
privilege”.

Otherwise, the script runs from normal user but we need only administrative rights.

20 | Page
Step 4: Go to Triggers and click New.

Step 5: Select ‘At Startup’ to begin the task. Select OK.

21 | Page
Step 6: Go to the Action bar and create a new action and give the path of python script.

In Action select Start Program and click on browse to open website_blocker.pyw file.

22 | Page
Step 7: Go to Conditions and unflag the power section. Press OK.

To run the python program when we’re on a battery on a laptop.

Step8: Here is the websiteblocker task which is ready to run when the system starts ON.

When we restart the computer, the program should run a python loop which will run every 5
seconds and the python program checks the hosts file. To test the program, right-click on the
program to run.

23 | Page
Now the status is showing as running

As we have made changes to computer it's better to restart.


If I try to open a YouTube or any website listed in hosts file

24 | Page
It shows Site can’t be reached. This proves that the process is running the python program
successfully in the background.

25 | Page
CHAPTER 3
VERIFICATION
Before Implementation of the Program:

26 | Page
After Implementation of the Program:

For verification of website blocking If we try to open a YouTube or any website listed in hosts
file

It shows Site can’t be reached. This proves that Website blocking using a python program is
running successfully at the background.

27 | Page
CHAPTER 4
CONCLUSION

Website blocking can help us overcome the temptation of distracting and addictive tech and cut
off access to the biggest distraction websites. Being secure in the online world becomes more
and more important every day and it is vital to protect our website and the data it holds now. In
today’s world the internet is full of information that can help us work more effectively.
Unfortunately, instant access to such an overwhelming amount of content can also backfire.
What starts as a quick jaunt to our social media feed or favorite website ends with dozens of
open tabs and hours of wasted productivity.
There are other ways to block certain websites like using third-party applications on your
computer. Some websites could be spreading viruses, contain explicit content or even be trying
to steal your personal data. In such cases, instead of trusting the applications. We can create our
own python script and customize code as per our requirement like duration and websites.
It is very much useful in blocking students/children’s/individual access to graphic content,
including violent images and unwanted websites by keeping them from being distracted by
entertainment and gaming sites.

28 | Page
REFERENCES
[1] https://www.internetsociety.org/resources/doc/2017/internet-content-blocking/ [2]
Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to
Programming.
[3] https://www.siteground.com/kb/how_to_use_the_hosts_file/
[4] https://www.skyfilabs.com/project-ideas/website-blocker-using-python
[5] https://blog.jdpfu.com/pages/hosts-for-security

29 | Page
30 | Page

You might also like