top of page

Block Websites with Python

  • Writer: Jesse Olchawa
    Jesse Olchawa
  • Oct 19, 2025
  • 3 min read

Introduction:

Hello there! Most recently I've been having a hard time focusing whilst working from home. My home computer being both my work hub and my gateway into the deep webs of the internet (primarily youtube with hour long documentaries on obscure vintage tech) has ended up making it difficult to focus back on work or personal projects. I've installed digital health blockers on my phone which has helped me get off of that but the main issue remains, the PC is a hard battle of discipline that I am slowly losing.


I've tried extensions that block sites for a period of time but I found them annoying to navigate and activate before I've had my coffee in the morning. They are also quite easy to switch off and only work on the browser theyre installed into. So I went looking for apps that can do what I need but found many to be paid/subscription based on Windows or worse require the internet to run which I found sketchy as all I wanted was a local way to block sites I've put in.


So over the weekend I've written a script which can do all that. It's easy to use but requires to be ran as admin in order to take effect.


I'm sharing it here to hope it can help someone else too!


The Guidelines:

When developing the script I wanted it to not only work but also:

  • be easy to use

  • be quick to run - one executable

  • be reversable and easy to switch off

  • track the minutes saved from doomscrolling


Setup:

Disclaimer: This application is for Windows ONLY.

You can either paste the script from here into your Python terminal and run.

RUN AS ADMIN

Ensure you started your terminal as administrator such as Visual Studio.


Or you can download the .zip from here and unzip it. Click the .exe to run inside the folder. RUN AS ADMIN

Zip Link:


Demo Video:

Here is a quick show of how it works, the blocking is global on the machine so no matter the browser the websites will no longer load. It may take some time to update on browsers such as Firefox so give it a minute or two and refresh as needed.


Start Guide:

Press the backup host file button to make a backup before you begin to block sites. This file contains data on how traffic is directed on your local machine so we need a backup copy in the event something goes wrong. In the event sites are not unblocking despite the button being pressed you can click the restore host button to restore everything to before you started to block. Changes can take a few minutes to work.


Adding Sites to the List:

The block list is a txt that contains all the sites loaded into the host file to block. This can be edited by the user by clicking on the button to find it then opening the file. The file is inside the installation directory and called block_list.txt. You can add a new website per line and save the file to be able to block those sites as well. You must reload the program for changes to take effect.


How it Works:

The script this builds upon is from is Patrick Loeber from Python Engineer.

Check out his site here for more great automation you can do with Python.


The script goes to the machines hosts folder and redirects traffic from those addresses to local host. The default local host address does not have a server connection (typically) so if loaded on a browser it shows a blank page. The unblocking process removes those sites from the file.


This script also keeps a counter keeps track of how long you have been blocking sites for and how many minutes you have saved by writing the minutes to the top of the block list file. The minutes saved data is read and returned every time the user unblocks the program and hits save minutes. The program must remain open to continue blocking. To check if the program is blocking the coloured box should shine green. If red the program is not blocking or has failed to do so.


Closing Notes:

This script has definitely helped me a lot already, I'm writing this blog post instead of watching another star trek next generation clips compilation after all! Discipline is important so let this tool help you reclaim your focus and put it into what really matters.


Comments


© 2025-2026 Jesse Olchawa

The content provided on this website cannot be utilised for any AI training or data gathering purposes!

bottom of page