Python Website Blocker Project

In this segment of the tutorial, we will be developing a real-time, highly sought-after Python application referred to as a website blocker. This application is designed to prevent users from accessing specified websites during designated timeframes.

Let’s explore the process of creating an application utilizing Python.

Objective

The purpose of the Python website blocker is to restrict access to specific websites that may divert the user’s attention during designated periods. In this application, we will prevent access to a predefined list of websites during working hours, allowing users to visit these sites only during their designated free time.

In this Python application, the designated working hours are from 9 AM until 5 PM. Any time outside of this interval will be regarded as free time.

Process

To restrict access to a particular website on the computer, it is necessary to modify the hosts file.

The hosts file

The hosts file is a local configuration file that was traditionally employed to associate hostnames with their corresponding IP addresses. While contemporary systems predominantly rely on DNS services for this hostname-to-IP address resolution, the hosts file remains a powerful tool that can be utilized for local IP address mapping configuration.

Location of hosts file

The location of the hosts file differs depending on the operating system in use.

Windows: C:\Windows\System32\drivers\etc

mac and Linux: /etc/hosts

Configuration

Given that the hosts file holds the associations between host names and their corresponding IP addresses, let us examine the contents of our hosts file, which is located at /etc/hosts on our CentOS 7 (Linux) system.

Image 1

As illustrated in the preceding image, the configurations are stored for the local server using the IP address 127.0.0.1.

In the same manner, it is possible to redirect any hostname to the local IP address (127.0.0.1). This action will effectively prevent access to that hostname, routing each request to the localhost instead.

To provide testimony, we will modify the contents of the hosts file by incorporating some of the subsequent lines. In order to make alterations to the /etc/hosts file, it is necessary to adjust its permissions.

Execute the command below in the terminal to modify the permissions.

Command:

Example

$ sudo chmod 777 /etc/hosts

Next, include the subsequent line in the file to restrict access to facebook.com (as an illustration).

Example

127.0.0.1		www.facebook.com

As shown in the image below.

Image 2

Save the file and then attempt to access www.facebook.com through your web browser.

Image 3

As illustrated in the figure above, the connection attempt was denied.

We have finalized our task by editing the hosts file manually. However, we have not yet reached our goal. Our goal is to restrict access to specific websites (such as Facebook) solely during business hours, which are from 9 AM to 5 PM.

A Python script is required that continually appends the required entries to the hosts file over a specified duration.

In this segment of the tutorial, we will create a Python script designed to modify the hosts file during operational hours. Additionally, we will set up this script to run automatically at system startup, eliminating the need for any manual execution.

Requirements

We need to know the following python modules to build the python website blocker.

  • file handling: file handling is used to do the modifications to the hosts file.
  • time: The time module is used to control the frequency of the modifications to the hosts file.
  • datetime: The datetime module is used to keep track of the free time and working time.

Input Required

This code uses input(). Please provide values below: