-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSiteBlocker.py
More file actions
36 lines (31 loc) · 1 KB
/
SiteBlocker.py
File metadata and controls
36 lines (31 loc) · 1 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
from pathlib import WindowsPath
# Get input on options
domain = input("Enter domain to manage: ")
unblock = '_'
while unblock != 'block' and unblock != 'unblock':
unblock = str(input("Block or unblock site? (block/unblock) "))
www = "_"
while www != 'y' and www != 'n':
www = input("Apply changes to www subdomain too? (y/n) ")
# Set variables
CONST_QUADS = '0.0.0.0 '
wwwsub = 'www.' + domain
# Locate and open the hosts file
folder = WindowsPath('C:/Windows/System32/drivers/etc')
file = folder / 'hosts'
# Write to file
# Remove hosts entry
if unblock == 'unblock':
f = open(file, mode='r')
lines = f.readlines()
f = open(file, mode='w')
for line in lines:
if line != CONST_QUADS + domain + '\n' and (line != CONST_QUADS + wwwsub + '\n' or www == 'n'):
f.write(line)
# Add hosts entry
else:
f = open(file, mode='a')
f.write(CONST_QUADS + domain + '\n')
if www == 'y':
f.write(CONST_QUADS + wwwsub + '\n')
print("Done!")