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 and Module only which comes by default with the Python Standard Library therefore you don’t need to install anything. time Datetime How do we block sites ? Every operating system has a hosts file and it’s here where we are going to add list of websites we want to block. We will add website URLs which are mapped to localhost thus preventing from accessing the real site, for instance: Instead adding we will add of 'www.facebook.com' '127.0.0.1 www.facebook.com' Therefore we need to add those site to host file during working hours and removing them immediately when it’s go home time. working_time: add mapped websites url to host file : remove the website files the host fle if else from Location of hosts file Host file that we need to edit are being stored on different path depending on Operating system you’re using For those in Linux: Linux_host = "/etc/hosts" For those in Windows: Window_host = r "C:\Windows\System32\drivers\etc\hosts" Building our Website Blocker Importing modules & pre configuring: time datetime datetime dt sites_to_block = [ , , , , , ] Linux_host = Window_host = r redirect = import from import as 'www.facebook.com' 'facebook.com' 'www.youtube.com' 'youtube.com' 'www.gmail.com' 'gmail.com' '/etc/hosts' "C:\Windows\System32\drivers\etc\hosts" "127.0.0.1" I have then made a simple function for adding and removing mapped website urls to the host file depending on working time . If your on Windows change the default_hoster to Window_host time datetime datetime dt sites_to_block = [ , , , , , ] Linux_host = Window_host = r default_hoster = Linux_host redirect = def block_websites(start_hour , end_hour): True: 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( ) open(default_hoster, ) hostfile: hosts = hostfile.read() site sites_to_block: site not hosts: hostfile.write(redirect+ +site+ ) : open(default_hoster, ) hostfile: hosts = hostfile.readlines() hostfile.seek( ) host hosts: not any(site host site sites_to_block): hostfile.write(host) hostfile.truncate() print( ) time.sleep( ) __name__ == : block_websites( , ) import from import as 'www.facebook.com' 'facebook.com' 'www.youtube.com' 'youtube.com' 'www.gmail.com' 'gmail.com' '/etc/hosts' "C:\Windows\System32\drivers\etc\hosts" "127.0.0.1" while if "Do the work ...." with 'r+' as for in if in ' ' '\n' else with 'r+' as 0 for in if in for in 'Good Time' 3 if '__main__' 9 18 The function receives two parameters. One is the starting time on which for testing I set as 09:00 and ending time for job which I primarily set 18:00. Therefore, you can adjust the testing to fit your own testing Hope you find this post interesting; don’t forget to subscribe to get more tutorials like this. To get the full code it on My Github In case of any suggestion or comment , drop it on the comment box and I will reply to you immediately. Previously published at https://kalebujordan.com/how-to-build-website-blocker-in-python/