Skip to content

Instantly share code, notes, and snippets.

@vb100
Last active July 11, 2017 20:14
Show Gist options
  • Save vb100/8ae0b6b73f1bd2f0ba1795d14ae54a10 to your computer and use it in GitHub Desktop.
Save vb100/8ae0b6b73f1bd2f0ba1795d14ae54a10 to your computer and use it in GitHub Desktop.
This test Python application blocking specific websites (in this example - Facebook.com) during works hours (8 AM - 5 PM) and allow to open these websites only after work.
import time
from datetime import datetime as dt
#Host to temporary file
host_temp="hosts"
#Host to original path file
hosts_path=r"C:\Windows\System32\drivers\etc\hosts"
redirect="127.0.0.1"
website_list=["www.facebook.com", "facebook.com"]
while True:
if dt(dt.now().year,dt.now().month,dt.now().day,0) < dt.now() < dt(dt.now().year,dt.now().month,dt.now().day,2):
print("Working hours...")
with open(hosts_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()
else:
print("Fun hours...")
with open(hosts_path,'r+') as file:
content=file.read()
for website in website_list:
if website in content:
pass
else:
file.write("\n"+redirect+" "+website+"\n")
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment