![]() With open(hosts_path, 'a ') as file: Opens the ' hosts' file again in read and append mode, for appending the websites those are not present in the file but presented in this python list, ' websites_list'. If the return value is False then the program follows the next step. If site in content: Checks if any website from ' website_list' is already presented in the ' hosts' file or not. With open(hosts_path, 'r') as file: Opens the ' hosts' file in read mode.įile.read(): Read the content of the ' hosts' file and stores into ' content' variable. The if statement checks if the current time is between the time we've set for preventing the access of those websites or not. Next, we declared a while loop for running infinitely. ![]() You can modify(add more or remove) it as per your requirement.ĭate_time.now(): Returns the current date and time, stores into a variable called 'curr'. Website_list: It's a python list containing three websites name I want to block from accessing. Hosts_path: It's a python dictionary containing the path of the 'hosts' file according to the OS. My_pc.system: Returns the OS name of the system, and based on this result we'll find the path of the 'hosts' file of the respective Operating System. If not any(site in line for site in website_list):Īs you can see, we imported three modules here, time, datetime, and platform. # Open the 'hosts' file in read and write mode. # Open the 'hosts' file in the read mode. If datetime(curr.year, curr.month, curr.day, 10) < \Ĭurr.now() < datetime(curr.year, curr.month, curr.day, 22): But how to do, right? Go to the next section where I described it for different OS. If it passed the previous step, then, you can set the program as a background process so that it runs automatically without interrupting other tasks. You can set the time as you need.įirst, run this program normally for testing purposes and check whether it's working or not. I've set the duration 10 AM to 10 PM in this program. Only use this code to block access to websites for a certain time duration. # Open the 'hosts' file again in read and append mode.įile.write("\n" redirect " " website "\n") # If the site isn't present there, add it to there Website_list = ['''# Open the 'hosts' file in the read mode. 'Windows': "C:\Windows\System32\drivers\etc\hosts", # The locations of the 'hosts' file according to the OS. '''A Python Program for Permanent Website Blocker: permanent_blocker.py''' Watch the entire video given below to change the permission of the host file in Windows. ➜File Location: "C:\Windows\System32\drivers\etc\hosts" Depending on your Operating System find out the solution given below. You'll not be able to modify this file as a normal user. The location of the host file depends on your OS We just need to modify this ' hosts' file by adding the hostnames(website addresses, you want to block) to it. It contains the names of several hosts, including their respective IP addresses. It's a simple text file, maps hostnames to IP addresses. ☛Learn Also : How to Send Emails using Python Program (Complete Tutorial) How does this Website Blocker work?Įvery operating system has a host file called 'hosts' locates in a fixed path depending on the operating system. It will block some mind destructive websites(users can set the list as per their requirements) permanently or temporarily(for a time duration: users can set the time as their choice). In this tutorial, we will create our own Website Blocker using Python to make this real-life problem grounded. Not only study, a harmful content or site can de-focused a mature person too from a productive work. Since study became online too, parents are concerned to keep their child from visiting any mind destructive websites or content. From study to work, internet become the most convenient way to stay connected with each other. ![]() Especially, after the pandemic(Covid) situation people have habituated with the internet. In this digital era, most of the facilities have became online. ![]()
0 Comments
Leave a Reply. |