-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebsite-Blocker.py
More file actions
63 lines (53 loc) · 1.93 KB
/
Website-Blocker.py
File metadata and controls
63 lines (53 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from datetime import datetime
import platform, os #To check OS type
ip_address = "127.0.0.1" #All Blocked website redirected to this URL
#Read websites from website_list.txt file
lists=open("website_lists.txt","r")
Website_lists = lists.readlines()
lists.close()
#Starting and end
Start_date = datetime(2022,12,13)
End_date = datetime(2022,12,14)
Today_date = datetime(datetime.now().year,datetime.now().month,datetime.now().day)
#Return the host path depending on operating system
def Host_path():
if platform.system() == "Windows":
hosts = r"C:/Windows/System32/drivers/etc/hosts"
else:
print("Unsupported OS detected.")
exit(0)
return hosts #Returns the location of hosts file
#Function of website blocker
def Blocker():
while True:#Loop for matching correct time
#Checking the current time is between the required hours
if Start_date <= Today_date < End_date:
print("Working Hours! Websites have been Blocked")
file = open(Host_path(),"r+")
content = file.read()
for website in Website_lists:
if website in content:
pass
else:
file.write(ip_address + " " + website + '\n')
file.close()
break
else:
print("Non-Working Hours! Websites have been Unblocked")
with open(Host_path(),"r+") as file:
content = file.readline(0)
file.seek(0)
for line in content:
if not any(website in line for website in Website):
file.write(line)
file.truncate()
break
#Main function
def main():
if platform.system() == "Windows":
Blocker()
else:
print("Incompatible OS")
exit(0)
if __name__ == "__main__":
main()