Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 1, 2020 04:34 pm GMT

How to build website blocker in Python

Hi guys

Many of us struggle to focus nowadays, easily distracted by Social media and some sites on the internet which dramatically affecting our productivity.

In this tutorial, you will learn and build your own website blocker to block certain selected sites that distract you during working hours.

Requirements

We are going to use the time and Datetime Module only which comes by default with the Python Standard Library therefore you dont need to install anything.

How do we block sites?

Every operating system has a host file and its on this file where we are going to add a list of websites we want to block by redirecting them to 127.0.0.1 (localhost).

We will add website URLs to the host file and mapping them to the localhost thus preventing you from accessing the real site during working hours.

Instead of adding www.facebook.com we are going to add 127.0.0.1 www.facebook.com, therefore whenever a user tries to access the website during working hours will be directed to the localhost.

Therefore we need to add those sites to the host files during working hours and removing them immediately when its going home time.

if working_time:  add mapped websites url to host file else:  remove the website files from the host file 

Location of host file

Host file that we need to edit is stored on different path depending on the Operating system youre using

For those in Linux

Linux_host = "/etc/hosts"

For those in window

Window_host = r"C:\Windows\System32\drivers\etc\hosts"

Building our Website Blocker

Importing modules & pre-configuring

import timefrom datetime import datetime as dtsites_to_block = [    'www.facebook.com',  'facebook.com',      'www.youtube.com', 'youtube.com',    'www.gmail.com', 'gmail.com']Linux_host = '/etc/hosts'Window_host = r"C:\Windows\System32\drivers\etc\hosts"redirect = "127.0.0.1"

We then required to add the sites to be blocked during working hours and remove them when it is not, I have made a simple function just to do that as shown in the source code below

Note
If your own Window change the default_hoster to Window_host

import timefrom datetime import datetime as dtsites_to_block = [    'www.facebook.com',  'facebook.com',      'www.youtube.com', 'youtube.com',    'www.gmail.com', 'gmail.com']Linux_host = '/etc/hosts'Window_host = r"C:\Windows\System32\drivers\etc\hosts"default_hoster = Linux_hostredirect = "127.0.0.1"def block_websites(start_hour , end_hour):    while True:        if dt(dt.now().year, dt.now().month, dt.now().day,start_hour)< dt.now() < dt(dt.now().year, dt.now().month, dt.now().day,end_hour):             print("Do the work ....")            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')        else:            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('Good Time')        time.sleep(3)if __name__ == '__main__':    block_websites(9, 18)

The function receives two-parameter, One is the starting time on which for testing I set as 9 am and the ending time for a job which I initially set 18 but you can twist it whatever you want.

When you run the above code, It will do as we have said, you can try accessing sites you listed in sites_to_block it will redirect you to the localhost.

If you find it interesting, Don't be shy share it with your fellow tech nerds so as more can learn this trick

The Original Article can be found on kalebujordan.com

GitHub logo Kalebu / Website-blocker-python

A simple website blocker project implemented in Python, It can be used to block certain websites during working time to reduce distraction thus improving productivity

Website-blocker-python

Intro

Hi Guys

This repo consist of a code of a simple website blocker project implemented in Python, It can be used to block certain websites during working time to reduce distraction thus improving productivity

The magic

The magic of this project lies on modifying the host file withing yourcomputer that manages how you access the web

Getting started

Well getting started with this project just clone the clone the repoand edit the host file location depending on the OS your using

    $ git clone https://github.com/Kalebu/Website-blocker-python    $ cd Website-blocker-python    Website-blocker-python $ 

Adding sites to block + Editing host files

Now open the app.py and the goto line 4 with variable site_to_blockand you can add the sites you would like to block during work time

Also when you go to the line no 12 and edit default host depending onOperating System you're using

One more


Original Link: https://dev.to/kalebu/how-to-build-website-blocker-in-python-a3f

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To