Skip to content

Instantly share code, notes, and snippets.

@justindmartin
Created April 20, 2013 18:33
Show Gist options
  • Save justindmartin/5426918 to your computer and use it in GitHub Desktop.
Save justindmartin/5426918 to your computer and use it in GitHub Desktop.
Script to block websites, which are distractions during coding
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