Automation Series - Article 2: Block WebsitesPython 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:
- Days: Monday to Friday
- Time: 1000 Hrs to 21 Hrs
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.
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.
Redicting the access to Website
Simply put, we just create a variable that stores the localhost ip address
Sites to block
Define a list of all the site that are to be blocked.
A sequential step-wise approach for lucidity
Step 1 : Get the current time from the system using the
dtclass that we’ve imported. This class has a few class methods (meaning that, these methods can be accessed using the class name itself) which can be found using
weekday()are 2 such functions that we can make use of without creating an object.
Step 2 : The
dt.now()method returns a (
datetime.datetime) object that contains all the details of the day of that moment and the time which can be accessed using keyword like
- The weekdays are enumerated in the modules as (‘Monday’,‘Tuesday’,…,‘Sunday’) = (0,1,…,7) => we’ve to check if the current day is between 0 & 5 as 6 and 7 stand for ‘Saturday’ & ‘Sunday’ respectively which are not a part of our constraint
Step 3 : Check if the value for the
dt.now()object are between 0 & 5 and 10 & 20, respectively
- Step 4 : If the condition in Step 3 is true, then open the
read and appendmode and write the redirects to the websites mentioned in the
block_site_listvariable above, one in each line.
Step 5 : If the condition in Step 3 is false,
Then open the
read and appendmode and read the contents of the file into a list using the
file.readlines()method and seek the pointer back to the start of the file using the
Iterate over the elements in the
contentvariable (which is a list as returned by the
For each value in the
contentvariable, check if it is existing in the
If yes, then simple skipp the value since we do not to block them anymore
If not, then it means that that particular line is operating system defined, hence, just write it back to the
Once done with writing back all the lines that are Operating system dependant, just truncate the remaining lines of the file.