Build a website blocker in Python in few minutes

Many of us struggle to focus nowadays, easily distracted by Social media and some sites on the internet which dramatically affecting our productivity.
In this tutorial, you will learn and build your own website blocker to block certain selected sites that distract you during working hours.
Requirements
We are going to use the time and Datetime Module only which comes by default with the Python Standard Library therefore you don’t need to install anything.
How do we block sites?
Every operating system has a host file and it’s on this site where we are going to add a list of websites we want to block.
We will add websites URL which are mapped to localhost thus preventing from accessing the real site, for instance
Instead of adding 'www.facebook.com' we will add '127.0.0.1 www.facebook.com'
Therefore we need to add those sites to the host file during working hours and removing them immediately when it’s going home time.
if working_time: add mapped websites url to host file else: remove the website files from the host fle
Location of the host file
Host file that we need to edit is being stored on different path depending on the Operating system you’re using
For those in Linux
Linux_host = "/etc/hosts"
For those in window
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
Building our Website Blocker
Importing modules & pre-configuring
import time
from datetime import datetime as dtsites_to_block = [
'www.facebook.com', 'facebook.com',
'www.youtube.com', 'youtube.com',
'www.gmail.com', 'gmail.com'
]Linux_host = '/etc/hosts'
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
redirect = "127.0.0.1"
I have then made a simple function of adding and removing the mapped websites URL to host file depending on working time.
If your own Window change the default_hoster to Window_host
import time
from datetime import datetime as dtsites_to_block = [
'www.facebook.com', 'facebook.com',
'www.youtube.com', 'youtube.com',
'www.gmail.com', 'gmail.com'
]Linux_host = '/etc/hosts'
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
default_hoster = Linux_host
redirect = "127.0.0.1"def block_websites(start_hour , end_hour):
while True:
if dt(dt.now().year, dt.now().month, dt.now().day,start_hour)< dt.now() < dt(dt.now().year, dt.now().month, dt.now().day,end_hour):
print("Do the work ....")
with open(default_hoster, 'r+') as hostfile:
hosts = hostfile.read()
for site in sites_to_block:
if site not in hosts:
hostfile.write(redirect+' '+site+'\n')
else:
with open(default_hoster, 'r+') as hostfile:
hosts = hostfile.readlines()
hostfile.seek(0)
for host in hosts:
if not any(site in host for site in sites_to_block):
hostfile.write(host)
hostfile.truncate()
print('Good Time')
time.sleep(3)if __name__ == '__main__':
block_websites(9, 18)
The function receives two-parameter, One is the starting time on which for testing I set as 9 am and the ending time for a job which I primarily set 18, but can adjust based on your own preference.
I also recommend you to check these;
- How to make a digital clock in Python
- How to track phone number in Python
- How to make a guessing game in Python
- How to program Arduino board with Python
- How to make your own music player in Python
- How to make a python GUI calculator in Python
Hope you find this post interesting, don’t forget to subscribe to get more tutorials like this. To get the full code it on My Github
In case of any suggestion or comment, drop it in the comment box and I will reply to you immediately.
Originally published at https://kalebujordan.com on May 18, 2020.