Skip to content

Instantly share code, notes, and snippets.

@musahibrahimali
Created February 5, 2022 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musahibrahimali/a3f851c7ff640515cbbeff1c5497fe4f to your computer and use it in GitHub Desktop.
Save musahibrahimali/a3f851c7ff640515cbbeff1c5497fe4f to your computer and use it in GitHub Desktop.
It is very good to control the websites a computer can visit especially when you want to control kid. in this gist we create a simple yet effective website blocker with python. the script can be rename to the .py extension to run as a normal python file. note that this is only texted on windows.
# the needed libraries
import time
from datetime import datetime as dt
# the host path
host_temp = r"C:\Users\<username>\folderName\Host File\hosts" # make a copy of the host file from the line below and replace this url string
host_path = r"C:\Windows\System32\drivers\etc\hosts" # The path to the original host file
redirect = "127.0.0.1"
website_list = [
# add as many websites as you want
"www.facebook.com",
"facebook.com",
"www.xnxx.com",
"xnxx.com",
"www.xvideos.com",
"xvideos.com"
]
# when to block the website and when to allow them
work_start_time = dt(dt.now().year, dt.now().month, dt.now().day, 8)
work_end_time = dt(dt.now().year, dt.now().month, dt.now().day, 16)
current_time = dt.now()
space = " "
new_line = "\n"
while True:
if work_start_time < current_time < work_end_time:
with open(host_path, "r+") as file:
content = file.read()
for website in website_list:
if website in content:
pass
else:
file.write(redirect + space + website + new_line)
# print("working hours....")
else:
with open(host_path, "r+") as file:
content = file.readlines()
file.seek(0)
for line in content:
if not any(website in line for website in website_list):
file.write(line)
file.truncate()
# print("Fun time fella....")
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment