Skip to content

Fixes #16 blocks website when app is running #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions app2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os

# Enter the site name which you want to block
sites_to_block = [
"www.facebook.com",
"facebook.com",
"www.youtube.com",
"youtube.com",
"www.gmail.com",
"gmail.com",
]

# different hosts for different os
Linux_host = "/etc/hosts"
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
default_hoster = Linux_host # if you are on Windows, then change it to Window_host
redirect = "127.0.0.1"

if os.name == 'posix':
default_hoster = Linux_host
elif os.name == 'nt':
default_hoster = Window_host
else:
print("OS Unknown")
exit()


def block_websites():
try:
print("Blocking websites...")
with open(default_hoster, "r+") as hostfile:
hosts = hostfile.read()
for site in sites_to_block:
if site not in hosts:
hostfile.write(redirect + " " + site + "\n")
print("Websites blocked. Now stay focused!")
except PermissionError as e:
print(f"Caught a permission error: Try running as admin. {e}")
exit()


def unblock_websites():
try:
print("Unblocking websites...")
with open(default_hoster, "r+") as hostfile:
hosts = hostfile.readlines()
hostfile.seek(0)
for host in hosts:
if not any(site in host for site in sites_to_block):
hostfile.write(host)
hostfile.truncate()
print("Websites unblocked. You're free now!")
except PermissionError as e:
print(f"Caught a permission error: Try running as admin. {e}")
exit()


if __name__ == "__main__":
try:
block_websites()
input("Press Enter to stop blocking and unblock websites...\n")
finally:
unblock_websites()