Blocking Websites with Python

Finding Motivation

Avoiding websites like Facebook, Instagram, Youtube and Reddit can feel like a full time job. The internet is a great tool for productivity and knowledge, but all too often it’s a time sink as well. Earlier this week, in a fit equally caused by frustration as much as inspiration, I decided to write a Python script that would allow me to automate the blocking process on my computer, as well as learn something new.

Aside on Willpower

Willpower may or may not be a finite resource. Anecdotally, decision fatigue feels real to me, but it may be just a mindset. I do know, however, that the allure of time sink websites like Facebook and Instagram keep me from important stuff, like y’know, my blog (¯(ツ)/¯). Therefore, for me, it makes sense to have some framework to disallow myself from wasting time, because I have a hard time making that decision by choice.


  • The Hosts file (stored at /etc/hosts) is a file that connects IP addresses with human readable names. An IP address is where a site is located, it can kind of be thought of as an actual address.
  • We can overwrite actual IP addresses with our own! This means that no matter what browser you use and type in we can re-route it before the connection is completed
  • In python to read and write to a file, the mode is ‘r+’
  • Since the /etc/hosts file is owned by the root directory, we need to use ‘sudo’ to edit it
  • Editing /etc/hosts isn’t an ideal way to block traffic, since the connection with the server (like is still started. A more thorough way to block traffic is to use IP Table rules. Thanks to this Stack Overflow post for helping me understand this!
  • Websites like Reddit have a ton of DNS resolutions, so we need to block ALL of them

That There Python Code

Here’s the whole script, we’ll break it down below.

With any script, it’s best to start reading by understanding the dependencies of the project. When you know what libraries a developer used can give you instant insight into the structure of a project. It’s kinda like a table of contents in a way.

Here, we are using sys and subprocess. From subprocess we’re importing call. This method allows us to call terminal commands like ls or nano! From sys, we’re using argv which keeps track of how many arguments we pass in at runtime.

Both block() and unblock() function in a similar manner. They open a file, check it for a certain line (# Block These Sites!), and if found either append or remove the following lines, in the cases of block() and unblock() respectively. Therefore, it’s important to append our changes to the bottom of our etc/hosts file to prevent overwriting important host info!

This code block does two things for us, it checks whether a user input true or false as an argument (block and unblock respectively) and then uses the call() method we discussed earlier to flush our cache, registering browsers like Chrome and Firefox to have to ask /etc/hosts for it’s updated state.


Last seen in his apartment in his boxers eating pizza over the sink like a rat. Approach with caution.

You may also like...

Leave a Reply

Your email address will not be published.