
Introduction
In today’s digital age, staying productive can be a real challenge, especially when there are countless distractions just a click away. Time-wasting websites, such as social media, video streaming platforms, and online games, can eat away at your precious time and hinder your productivity.
To combat this issue, you can create a time-wasting website blocker using Python. In this article, we will guide you through the process of building your own website blocker to help you stay focused and make the most of your time.
Why Block Time-Wasting Websites?
Time-wasting websites can be a major productivity killer. Whether you’re working on a project, studying for an exam, or simply trying to complete tasks efficiently, the allure of these websites can be hard to resist. Blocking them during specific periods can significantly improve your focus and productivity. Additionally, it can help in managing screen time for individuals and promoting a healthier online presence.
How this Website Blocker works?
The ‘hosts’ file is a plain text file in computer systems, including Windows, macOS, and Linux, that serves as a local DNS (Domain Name System) resolver. Its primary purpose is to map human-readable domain names to IP (Internet Protocol) addresses. This mapping allows your computer to resolve domain names to their corresponding IP addresses without needing to query a DNS server on the internet.
By modifying it, you can redirect specific websites to localhost or another address, effectively blocking access. In this process, we’ll first find the ‘hosts’ file on our computer and change its permissions. After that, we’ll employ our Python program to make modifications to the ‘hosts’ file, effectively blocking the specified websites.
Locate the ‘hosts’ file
Based on your Operating System, explore the solution provided below.
Windows
Location: `C:WindowsSystem32driversetchosts`
The video offers a detailed, step-by-step tutorial on how to modify the permissions of the ‘hosts’ file in Windows.
Linux and MacOS
Location: `/etc/hosts`
To start, verify the file’s permissions. If the permissions appear as “-rw-r–r–” you’ll need to modify them by granting group write permission.
Open the terminal and type this command: `sudo chmod 777 /etc/hosts`.
You can return to the previous state (“-rw-r–r–“) by executing the following command: `sudo chmod 644 /etc/hosts`.
Writing the Code
Now, create a Python script that allows you to schedule the blocking and unblocking of these websites. You can use Python’s `datetime` module to set up a schedule. Here’s a simplified script:
import time
import platform
from datetime import datetime
my_pc = platform.uname()
# Get your OS name
OS = my_pc.system
# The locations of the 'hosts' file according to the OS.
hosts_path = {
'Linux': "/etc/hosts",
'Windows': "C:WindowsSystem32driversetchosts",
'Mac': "/private/etc/hosts"
}
# IP address of the localhost.
redirect = "127.0.0.1"
# Websites list those you want to block. You can Modify it.
website_list = ['www.facebook.com', 'www.twitter.com', 'www.instagram.com']
dt = datetime.now()
while True:
if datetime(dt.year, dt.month, dt.day, 8)<dt.now()<datetime(dt.year, dt.month, dt.day, 16):
# print("Working hours...")
# Open the 'hosts' file in the read mode.
with open(hosts_path[OS], 'r') as file:
# Read the content of the 'hosts' file.
content = file.read()
for site in website_list:
# Check if the website is already in the
# 'hosts' file or not
if site in content:
pass
else:
# If the site isn't present there, add it
# Open the 'hosts' file again in read and append mode.
with open(hosts_path[OS], 'a+') as file:
file.write(redirect + " " + site + "n")
else:
# Open the 'hosts' file in read and write mode.
with open(hosts_path[OS], 'r+') as file:
content = file.readlines()
file.seek(0)
for line in content:
if not any(site in line for site in website_list):
file.write(line)
file.truncate()
# print("Fun hours...")
time.sleep(5)
This script checks the time and, during your specified working hours (in this example, 8 AM to 4 PM), it redirects the listed websites to localhost, effectively blocking access. Outside of working hours, it unblocks them.
Save the Python script with this name: “website_blocker.py” and run it. Ensure it runs continuously in the background.
Run the Python Program as a Background Process
Running a Python program as a background process on Windows, Linux, and macOS involves slightly different methods for each operating system. Here’s how you can do it on each platform:
Windows
On Windows, you can use the Task Scheduler to run the website blocker script as a background process. Here’s how:
- Save your Python website blocker script (the one we discussed earlier) with a `.py` extension.
- Open the Task Scheduler:
- Press `Win + R` to open the Run dialog.
- Type `taskschd.msc` and press Enter.
- In the Task Scheduler window, click on “Create Basic Task” in the right-hand pane.
- Follow the wizard to give your task a name and description. Click Next.
- Choose “Daily” or “Weekly,” depending on how often you want the script to run. Click Next.
- Set the start date and time. Click Next.
- Choose “Start a program” and click Next.
- Browse and select your Python script (`.py` file). Click Next.
- Review your settings and click Finish.
- To run the task in the background, you can leave the “Open the Properties dialog for this task when I click Finish” option unchecked and click Finish.
Your website blocker script will now run as a background task based on the schedule you set.
Linux and macOS
On Linux and macOS, you can use the terminal and the `nohup` command to run the website blocker script as a background process:
- Open a terminal window.
- Navigate to the directory where your Python website blocker script is located, or specify the full path to your script.
- Use the following command to run your script in the background: `nohup python website_blocker.py &`. [`nohup` stands for No Hangup. It means that the process won’t be terminated even if the parent process is killed, and it will generate a ‘nohup.out’ file in the same directory as the program file. On the other hand, using an ampersand (`&`) at the end of a command signifies running the program in the background.]
- After running this command, you will see a message like “nohup: ignoring input and appending output to ‘nohup.out'”. This means your script is running in the background.
- You can close the terminal, and your website blocker script will continue to run in the background until it completes or you stop it manually.
Video Tutorial and Output
The video will demonstrate both the results of the program mentioned above and the instructions to convert it into a startup process.
Remember that running the website blocker script in the background without a terminal session will make it harder to interact with the script directly. You may want to implement logging or other mechanisms to monitor its progress and results. Also, ensure that the script is running with the necessary permissions to modify the `hosts` file and perform the blocking action.
Summary
In this article, we’ve created a Python program to act as a website blocker. Initially, we adjust the permissions of our computer’s `hosts` file. Then, our Python script accesses this file to enable the scheduled blocking and unblocking of specific websites.
This Website Blocker can be a valuable tool for boosting productivity and maintaining focus on your tasks. You have the flexibility to customize the list of blocked websites and the scheduling to align with your specific requirements.
Towards the conclusion, we’ve also learned how to set up this program as a background process, ensuring it runs automatically and enhances productivity during your dedicated work hours. So, begin using your website blocker today and regain control o