-
-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathWebsite_Blocker.py
More file actions
61 lines (55 loc) · 1.5 KB
/
Website_Blocker.py
File metadata and controls
61 lines (55 loc) · 1.5 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
Run this script as root
import time
from datetime import datetime as dt
# change hosts path according to your OS
hosts_path = "
/etc/hosts & quot
# localhost's IP
redirect = "
127.0.0.1 & quot
# websites That you want to block
website_list =
[ & quot
www.facebook.com & quot, & quot
facebook.com"
,
& quot
dub119.mail.live.com"
, & quot
www.dub119.mail.live.com"
,
& quot
www.gmail.com"
, & quot
gmail.com"
]
while True:
# time of your work
if dt(dt.now().year, dt.now().month, dt.now().day, 8)
<
dt.now() & lt
dt(dt.now().year, dt.now().month, dt.now().day, 16):
print( & quot
Working hours... & quot
)
with open(hosts_path, 'r+') as file:
content = file.read()
for website in website_list:
if website in content:
pass
else:
# mapping hostnames to your localhost IP address
file.write(redirect + "
& quot
+ website + "
\n & quot
)
else:
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)
# removing hostnmes from host file
file.truncate()