-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathselfcontrol.py
More file actions
41 lines (33 loc) · 1.17 KB
/
Copy pathselfcontrol.py
File metadata and controls
41 lines (33 loc) · 1.17 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
# a simple script to block websites
from datetime import datetime
end_time = datetime(2021, 1, 1, 20) # y, m, d, h, min
sites_to_block = ['www.facebook.com', 'facebook.com']
hosts_path = '/etc/hosts'
#r"C:\Windows\System32\drivers\etc\hosts"
redirect = "127.0.0.1"
def block_websites():
if datetime.now() < end_time:
print("Block sites")
with open(hosts_path, 'r+') as hostfile:
hosts_content = hostfile.read()
for site in sites_to_block:
if site not in hosts_content:
hostfile.write(redirect + ' ' + site + '\n')
else:
print('Unblock sites')
with open(hosts_path, 'r+') as hostfile:
lines = hostfile.readlines()
hostfile.seek(0)
for line in lines:
if not any(site in line for site in sites_to_block):
hostfile.write(line)
hostfile.truncate()
# sudo python main.py
if __name__ == '__main__':
block_websites()
# 1. Trigger script manually every now and then
# 2. Cron job
# 3. scipt running in background with while True
#while True:
# block_websites()
# time.sleep(5)