Skip to content

Instantly share code, notes, and snippets.

@PixelRobot
Created January 9, 2014 23:49
Show Gist options
  • Save PixelRobot/8344331 to your computer and use it in GitHub Desktop.
Save PixelRobot/8344331 to your computer and use it in GitHub Desktop.
A little script for blocking or unblocking sites editing your hosts file. It can't add lines, but it comments or uncomments it to unblock or block sites.
from colorama import init, Fore
from os import system, name
init()
route = "/etc/hosts"
f = open(route)
lines = f.readlines()
f.close()
def cls():
system(["clear","cls"][name == "nt"])
def save():
f = open(route, "w")
for line in lines:
f.write(line)
f.close()
def list():
cls()
i = 0
for line in lines:
if (line[0] == "#"):
print Fore.GREEN,
else:
print Fore.RED,
print str(i) + ". " + line,
i = i + 1
print Fore.RESET
def block(n):
line = lines[n]
if lines[n][0] == "#":
lines[n] = lines[n][2:]
def unblock(n):
if lines[n][0] != "#":
lines[n] = "# " + lines[n]
def blockall():
i = 0
for line in lines:
if line[0] == "#":
lines[i] = line[2:]
i = i + 1
def unblockall():
i = 0
for line in lines:
if line[0] != "#":
lines[i] = "# " + line
i = i + 1
list()
while True:
option = raw_input("\nOption: ").lower().split()
if option[0] == "bye" or option[0] == "exit":
save()
exit()
elif option[0][0] == "s":
save()
list()
elif option[0][0] == "b":
if (option[1][0] == "a"):
blockall()
else:
block(int(option[1]))
list()
elif option[0][0] == "u":
if (option[1][0] == "a"):
unblockall()
else:
unblock(int(option[1]))
list()
else:
print "I don't know what you want."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment