Coders Packet

Website Blocker using python

By Mehul Gupta

In this program, we will pass the link of websites that you believe are distracting and the program will block such websites while you are working on your computer.

1. Every system has a host file whether it is Mac, Windows, or Linux. Host paths are defined differently in OS systems that handle the web pages and programs.



2. The host file is an operating system file that maps hostnames to IP addresses. In this application, we will map website hostnames to our local host address. We will write the hostname in hosts.txt using Python file manipulation handling and remove the lines after your working hours.

3. In the code file(attached), Change the end_time and set the time until all of your sites are blocked. In the sites_to_block list, specify all of the websites you would like to be restricted. Windows users must also change the host's file's correct path (see the commented out path). Then you have three choices for how to use it:

  • Every now and again, manually trigger the script.
  • Create a Cron job.
  • With a while True loop, leave the script running in the background.

4. The technique is simple. Only the host file is modified, and a mapping from the restricted domain name to our IP address is created. All domain requests are directed to our localhost in this approach. Most of the time, there is no server running on localhost, therefore we get a "Site can't be reached" response. Create a backup duplicate of your host's file before experimenting with this code and modifying it.

This is a real-world application that restricts access to distracting websites such as Facebook, YouTube, and others during working hours.

Download Complete Code


No comments yet