- Published on
Python Script to Block Websites
- Authors
- Name
- Yashraj Singh
- ysraz-singh
Table of Contents
Introduction
- 1.1 Exploring Domain Blocking with Python Script
- 1.2 Prerequisites for Domain Blocking
- 1.3 The Power of Python and Administrative Access
Understanding Website Blocking
- 2.1 Why Block Websites?
- 2.2 Benefits of Blocking Websites
The Python Script
- 3.1 Explanation of the script's code and usage
- 3.2 Simple Domain Blocking Without Third-Party Libraries
Customization
- 4.1 Modifying the Target Domain or IP Address
- 4.2 Flexibility for Personalized Domain Blocking
Verification
- 5.1 Ensuring Your Block Is Effective
- 5.2 Two Verification Methods
- 5.2.1 Browser Verification
- 5.2.2 Terminal Ping Test
Conclusion
Introduction
Welcome to our step-by-step guide on domain blocking using a Python script. In this article, we'll explore the world of domain control without the need for third-party libraries. All you need is a basic understanding of Python and the root permissions for your system. Root permissions are essential for this process, and on Linux and macOS, you can use 'sudo,' while on Windows, administrative access to the terminal is required.
Understanding Website Blocking
Website blocking is a useful technique for managing internet access and productivity.
The Python Script
In the following sections, we'll delve into the Python script responsible for blocking domains. This script is straightforward, efficient, and requires no external libraries. It enables you to control your internet experience by blocking specific domains or IP addresses.
import platform
# Define the domain or IP address you want to block
target = "www.google.com" # Change this to the domain or IP you want to block
# Determine the path to the host file based on the operating system
system = platform.system()
if system == "Windows":
hosts_path = r"C:\Windows\System32\drivers\etc\hosts"
else:
hosts_path = "/etc/hosts"
# Open the host file in append mode and add an entry to block the target
with open(hosts_path, "a") as hosts_file:
hosts_file.write(f"\n127.0.0.1 {target}\n")
print(f"Blocked {target} in the host file.")
# Note: On Linux and macOS, you may need to run this script with sudo for write access to /etc/hosts.
As seen in the code, we've humorously chosen www.google.com as our target domain. We then access the hosts file and register this domain, effectively blocking it on our system.
Customization
Customization is at your fingertips. If you wish to block a different IP or domain, simply modify the 'target' variable. This flexibility empowers you to tailor the script according to your specific needs.
Verification
To verify whether the domain is successfully blocked, you have two options:
Browser Verification: Enter the domain in your browser's search bar and hit Enter. If the page doesn't load, your domain block is working.
Terminal Ping Test: Open your terminal and enter
ping www.google.com
(replace with your domain). If it shows the local IP address (127.0.0.1), your domain is effectively blocked. Any other IP address indicates that the block is not working.
Conclusion
In this article, we've empowered you to take control of your internet experience by blocking domains using a Python script. We've covered the script's inner workings, discussed customization options, and provided verification methods to ensure your block is effective.
Remember, with great power comes great responsibility. Use domain blocking responsibly and exclusively for legitimate purposes, as it can impact both security and functionality. Whether you're looking to enhance online safety or minimize distractions, this Python script offers a practical solution to achieve your goals. Happy domain blocking!