Created
April 20, 2013 18:33
-
-
Save justindmartin/5426918 to your computer and use it in GitHub Desktop.
Script to block websites, which are distractions during coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
#open hosts file and read current data | |
hostsFile = open("/etc/hosts", "w+") | |
hostsFileData = hostsFile.read() | |
#host list | |
newHosts = [ | |
"facebook.com", | |
"quora.com", | |
"reddit.com", | |
"tumblr.com", | |
"twitter.com" | |
] | |
#loop through host list | |
for host in newHosts: | |
for i in range(0,2): | |
#add www subdomain, if second run on host | |
if i == 1: | |
host = "www." + host | |
#build host entry | |
hostEntry = "127.0.0.1\t" + host + "\n" | |
#if undo, revert to previous host file | |
if len(sys.argv) > 1 and str(sys.argv[1]).lower() == "undo": | |
hostsFileData = hostsFileData.replace(hostEntry,"") | |
#else, push new host entries | |
else: | |
hostsFileData += hostEntry | |
#write new hosts file and close handle | |
hostsFile.write(hostsFileData) | |
hostsFile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment