We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
tay
Website Blocker
wing Python
aa Step by Step
Explanation
creIdea and Approach
Step-1: Import time, datetime modules
for working with dates and times.
Step-2: Set the path to the hosts file in
your PC and define websites to block.
This file is used to map domain
name(Website) to custom IP address
also define list of websites, working
hours during which you want to block.
Step-3: Set the IP address(127.0.0.1) to
the blocked websites in the hosts file,
so that sites will be redirected to this
address when we open it which
eventually shows “This site can’t be
reached”.
@Code.with.EVIdea and Approach
Step-4: Check current time, If the
current time falls within the working
hours, the code opens the hosts file and
checks if each website in the
websites_to_block list is already present
in the file. If not, it writes a line to the file
that redirects the website to the
redirect_ip (127.0.0.1). This effectively
blocks access to websites.
Step-5: Otherwise, the code opens the
hosts file and Removes any blocked
website entries from previous blocking
periods.
Step-6: Repeat this process & introduce
some delay to avoid excessive CPU
usage.
@Code.with.EVComplete Implementation
cee
ce ae eee sued
eee man EC aS)
eel ee CEC aN CEC ce
# List of websites to block
Rone ol ge CL CUS]
Ce hee sd
Pietra et
aN Ree C1)
ema ee OM LETT
eres ee ee Oe eee
print(start_time)
Pec a
Pee eR eeu eR ca ear Cr
Rice eae ene eam
Patera Ie eee)
with open(hosts_path, "r+") as file:
content = file.read( )
ete eR icc omc ma
ear eno
ae ea ee a)
foe
else:
# Block the website by redirecting it to the specified IP
file.write(redirect_ip +" " + website + "\n")
et
eC ey
Cen
PUT)
Mie Cee ees eae
Cae gtem eC)
aed)
CnC OR- cs)
Caer te ORCC Eat eet com soe
faCetecctactry)
Ce eR ee eC iCU RaeCsnr Bay
file. truncate()
# Check every 5 seconds
Paucar)
Instead of manually running daily. Use Task
Scheduler to run your Python script daily.
@Code.with.EV| Post About Tech and
Coding
Follow for more content like this
@Code.with.EV