Website Blocker in Python:

It is a very useful program that is there for blocking certain sites like Facebook and Youtube using python for data science during working hours.

In this program we will pass links of websites that we want to block during working hours and the program will block these or that time.

The architecture of the program:
  1. Every system has a host file irrespective of Mac, Windows or Linux.

Host file for Mac and Linux:


Host file for Windows:

  1. Working of the host file: the host is an operating system file that maps the hostnames to the IP addresses. In the following program, we will be mapping hostnames of websites to our localhost addresses. by using the Python host manipulation, we will be writing the hostname in hosts.txt, and we will remove the lines after our working hours.
Host file in Mac:

Website Blocker in Python for Data Science - PST Analytics Website Blocker in Python for Data Science - PST Analytics

Note for Windows users: We need to create a duplicate of the OS’s host file. Then we should provide the path of the duplicate file in hosts_path which is mentioned in the script.

Scheduling the above script in Mac: We have to open crontab in terminal as a root.

  1. First, write the following in the terminal:

sudo crontab  -e

The terminal will look like this.

Website Blocker in Python for Data Science - PST Analytics

  1. Next, we will press ‘i’ for going to insert/editing mode and then we should write @reboot_script_path.
  2. Next, we should save the tab by pressing esc for quitting the writing mode and falling back to command mode and later write “:wq” and at last press enter for validation.
  3. Restart the system, and the program shall show its application.

Scheduling in Windows: It is tricky but not that hard.

  1. First, we should change the extension of our script from “.py” to “.pyw”.
  2. Next, we should open the task scheduler. The task scheduler will look as below.

  1. After that, click on “create task”. We should fill it with a name and flag it as “Run with highest privilege”.

Website Blocker in Python for Data Science - PST AnalyticsWebsite Blocker in Python for Data Science - PST Analytics

  1. Now we will go to triggers, and we will select “At startup” to begin the task.

  1. Next, we will go to the action bar and create a new action and give the path of the script.

  1. We will go to the conditions bar, and we will unflag the power section.

  1. As soon as we press OK we can see the script has been scheduled.

  1. Now we will restart our computer and see the changes.

If you want to learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.