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:
- Every system has a host file irrespective of Mac, Windows or Linux.
Host file for Mac and Linux:
Host file for Windows:
- 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:
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.
- First, write the following in the terminal:
sudo crontab -e
The terminal will look like this.
- Next, we will press ‘i’ for going to insert/editing mode and then we should write @reboot_script_path.
- 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.
- Restart the system, and the program shall show its application.
Scheduling in Windows: It is tricky but not that hard.
- First, we should change the extension of our script from “.py” to “.pyw”.
- Next, we should open the task scheduler. The task scheduler will look as below.
- After that, click on “create task”. We should fill it with a name and flag it as “Run with highest privilege”.
- Now we will go to triggers, and we will select “At startup” to begin the task.
- Next, we will go to the action bar and create a new action and give the path of the script.
- We will go to the conditions bar, and we will unflag the power section.
- As soon as we press OK we can see the script has been scheduled.
- Now we will restart our computer and see the changes.