DEV Community

Cover image for How to build website blocker in Python

How to build website blocker in Python

Jordan Kalebu on October 01, 2020

Hi guys Many of us struggle to focus nowadays, easily distracted by Social media and some sites on the internet which dramatically affecting our p...
Collapse
 
vampiire profile image
Vamp • Edited

neat article thanks for sharing. an alternative approach is to create a hosts.blocked file. then use a cron job (or python) to simply swap the files:

create two files:
hosts.original (copy of original etc hosts)
hosts.blocked (original + deadend for blocked sites)

(on)
copy hosts.blocked to etc hosts

(off)
copy hosts.original to etc hosts

this will ensure you are not mutating the original file. you can then convert your tool to generate the .blocked file based on that list of sites (auto appending common prefixes like www)

Collapse
 
mehemmedmehdipy profile image
MehemmedMehdiPY

Hello. I registered on the website newly in order to get some help about this blog. Firstly, this article is so understandable including code part. However, questions are inevitable. I spent several hours to understand the project, but couldn't get why we should use truncate(), seek(), and write() (to rewrite) in "else" part. I searched how the first two functions work, and tried trial and error tactics to understand the functionality of code. In conclusion, only "else" part of python code is not comprehensive to me. I would be glad if I get help. Thanks in advance

Collapse
 
d1p profile image
Debashis Dip

Or, remove the date time check from python code and run the script as a cronjob and then you don’t need to run the script and open the file over and over.

Collapse
 
theviklink profile image
TheVikLink

From where do I run this program?

Collapse
 
kalebu profile image
Jordan Kalebu

For testing just run it as a program ... but for it to keep running for quite a while in your background, It's advised to run it as a process

Collapse
 
swayamkulkarni18 profile image
Swayam Kulkarni

Hi!!👌🖐️ thanks bro!

Collapse
 
kalebu profile image
Jordan Kalebu

You're welcome @Swayam-KULKARNI

Collapse
 
vedantmadane profile image
Vedant Madane

PermissionError: [Errno 13] Permission denied: 'C:\Windows\System32\drivers\etc\hosts'

Collapse
 
kalebu profile image
Jordan Kalebu

You need to run the python script with administrator privileges, Try checking out on this StackOverflow comment they explained how to deal with it

stackoverflow.com/questions/130763...

Collapse
 
emanuel937 profile image
Emanuel937

you will need root permission to run this code

Collapse
 
emanuel937 profile image
Emanuel937

this article is copy and paste ......

Collapse
 
alvinjfguo profile image
alvinjfguo

i can run the program, it prompted a cmd window thing with the "do the work" text, but i can still open facebook. how to fix this?

Collapse
 
kalebu profile image
Jordan Kalebu

Sometimes the issue can be you're accessing a subdomain that is not listed on the sites to be blocked, can you access all the sites in sites to be blocked list or is it just Facebook?

Collapse
 
punkindapie profile image
ThiccPie

I know am kinda late but do I need to create a folder for this script if I want it to run cause for the add mapped website url to host file part it comes up with a syntax error any help?

Collapse
 
kalebu profile image
Jordan Kalebu

Can you share your codebase?

Collapse
 
skipperhoa profile image
Hòa Nguyễn Coder

Nice, thanks you!

Collapse
 
stokry profile image
Stokry

Nice, thanks!

Collapse
 
yasthir01 profile image
Yasthir Dhewnarian

How exactly do you deactivate it? The script worked, and yes the websites are now blocked... But how do I bring them back without waiting until the time expires?

Collapse
 
kalebu profile image
Jordan Kalebu

You should change hijack the script manually by changing the working hours in the block_websites function to a range which outside of your current time and then the script will think it's resting time, will go and remove the block websites in the host file.