Automation Series - Article 2: Block Websites

Python Prose Tech

There are two kinds of procrastinators in humans as categorised by Tim Urban in one of his Ted Talks basing on their actions, on the approach of the due date. Type-1, are those who barely react even when the due date passes by and then the Type-2, are just most of us. we all have something called as the ‘Panic Monster’ that wakes up right at the eleventh hour. Until then even if our mind reminds us of the due date, often, the (mental) ‘Monkey’ that is having the control / steering wheel, wouldn’t give up. Which is why we end up doing some random stuff like watching youtube videos or playing games even if the due date is next day. Right at the eleventh hour, the panic monster comes into consciousness and we all know what happens next. We find no difference between day and night or hunger or thirst until we complete the task or at least almost complete the task.

Therefore, I decided, to keep myself restricted of the options that my (mental) monkey has and to block those sites for most part of the days and it is then I learnt how to block websites from this article on GeeksforGeeks.

Note: I took the basic idea of what to do from the above mentioned blog and customised for my requirements which we shall look into. Again, I’ll take a line by line approach.


Websites in the block_site_list variable below should not be accessible for:



import time
from datetime import datetime as dt

The first part of a python program is just about importing the required packages. Here, I have imported time, again for the only reason, to make the program sleep for some time and it’s the datatime package that the code is essentially dependant on just like Delay Start options for startup applications.

Then the datetime class is imported from the datetime module with the alias dt (alias is just to satisfy my laziness).

Theory of Routing

Since this topic doesn’t come under the scope of this article, we’ll directly get to the concept to understand.

Whenever a web browser tries to access a website, it is redirected through the hosts file in the local machine that checks, if that website is defined in the file. If it is defined, it means that, that site is being served / hosted locally. Implying that at that ip address, the machine that is being used, serves web applications that are running locally and that is how the ip address / localhost or in other words are defined as the address of the internal server. If not, then the request proceeds through the internet to the DNS and further which is out of the scope of this article.

These declarations are defined in the file called as hosts, in most linux distros if not all, also in macOS and Windows with the difference between linux and Windows based operating systems being the location where the hosts file is stored / saved / placed.

This article is being discussed with the location details of the file for Linux based distros or Debain based distros to be more precise.

hosts file path

Find the path to the hosts file in your machine and store that in a variable as below as we’re going make modifiications to that file, precisely.

hosts_path = '/etc/hosts'

Redicting the access to Website

Simply put, we just create a variable that stores the localhost ip address

redirect = ''

Sites to block

Define a list of all the site that are to be blocked.

block_site_list = [
    '', '', '', '',
    '', '', '',
    '', '', '', ''

Program Logic

A sequential step-wise approach for lucidity

if 0 <= dt.weekday( < 5 and 10 <= <= 20:
with open(hosts_path, 'r+') as file_ob:
    content =
    for site in block_site_list:
        if not site in content:
            file_ob.write(redirect + " " + site + "\n")
with open(hosts_path, 'r+') as file_ob:
    content = file_ob.readlines()
    for line in content:
        if not any(site in line for site in block_site_list):

Youtube Playlist

Wanna reach out? Email me!

Copyright (c) 2021 Sai Gautam Kolluru