Website Blocker Using Python Last Updated : 08 Jul, 2022 Comments Improve Suggest changes Like Article Like Report This is real world program which blocks certain distracting website like Facebook, Youtube etc during your work hours. About the program : What we are going to in this program is that we will pass the link of websites which you think is distracting and the time that you are working on your computer and program will block those website. Program Architecture: Every system have host file whether it is Mac, Windows or Linux. Host file in Mac and Linux : /etc/hosts Host file in Windows: C:\Windows\System32\drivers\etc Working of host file: Host is an operating system file which maps hostnames to IP addresses. In this program we will be mapping hostnames of websites to our localhost address. Using python file handling manipulation we will write the hostname in hosts.txt and remove the lines after your working hours. Host file in Mac: Python # Run this script as root import time from datetime import datetime as dt # change hosts path according to your OS hosts_path = "/etc/hosts" # localhost's IP redirect = "127.0.0.1" # websites That you want to block website_list = ["www.facebook.com","facebook.com", "dub119.mail.live.com","www.dub119.mail.live.com", "www.gmail.com","gmail.com"] while True: # time of your work if dt(dt.now().year, dt.now().month, dt.now().day,8) < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day,16): print("Working hours...") with open(hosts_path, 'r+') as file: content = file.read() for website in website_list: if website in content: pass else: # mapping hostnames to your localhost IP address file.write(redirect + " " + website + "\n") else: with open(hosts_path, 'r+') as file: content=file.readlines() file.seek(0) for line in content: if not any(website in line for website in website_list): file.write(line) # removing hostnmes from host file file.truncate() print("Fun hours...") time.sleep(5) Special note for Windows users : Windows user need to create a duplicate of OS's host file. Now provide the path of the duplicate file in hosts_path mentioned in the script. Scheduling above script in Mac : For scheduling above script in Mac you have to open crontab in your terminal as a root. Write following command in terminal: sudo crontab -e Your terminal should look like this: Now press "i" to go into insert/editing mode and write @reboot python_script_path . Save the tab by pressing first esc to quit write mode and fall back to command mode and now write ":wq" and finally press enter to validate. Restart your system and see the magic. Scheduling in Windows: Scheduling of above script is little bit trick but I will guide you step by step- First of all change the extension of your script from ".py" to ".pyw". Now open task scheduler. Task scheduler should look like this: You may see website blocker already scheduled because I have already scheduled in my computer for my testing purpose. Follow further instruction of scheduling carefully in order to schedule website blocker in your computer. Click on "create task". Fill the name of your choice and flag "Run with highest privilege". Now go to triggers, select "At startup" for begin the task. Go to Action bar and create a new action and give path of your script. Go to conditions bar and unflag the power section. Press ok and you can see the script scheduled. Finally restart your computer and see the magic. Note: You can also check instantly by clicking run button. Comment More infoAdvertise with us Next Article Website Blocker Using Python A Abhishek Verma 16 Follow Improve Article Tags : Technical Scripter Python Web Technologies python-utility Python-projects +1 More Practice Tags : python Similar Reads Image Scraping with Python Scraping Is a very essential skill for everyone to get data from any website. In this article, we are going to see how to scrape images from websites using python. For scraping images, we will try different approaches. Method 1: Using BeautifulSoup and Requests bs4: Beautiful Soup(bs4) is a Python l 2 min read Python script to monitor website changes In this article, we are going to discuss how to create a python script to monitor website changes. You can code a program to monitor a website and it will notify you if there are any changes. This program has many useful scenarios for example if your school website has updated something you will com 3 min read Scraping websites with Newspaper3k in Python Web Scraping is a powerful tool to gather information from a website. To scrape multiple URLs, we can use a Python library called Newspaper3k. The Newspaper3k package is a Python library used for Web Scraping articles, It is built on top of requests and for parsing lxml. This module is a modified an 2 min read File Sharing App using Python Computer Networks is an important topic and to understand the concepts, practical application of the concepts is needed. In this particular article, we will see how to make a simple file-sharing app using Python.  An HTTP Web Server is software that understands URLs (web address) and HTTP (the proto 4 min read Automated Website Scraping using Scrapy Scrapy is a Python framework for web scraping on a large scale. It provides with the tools we need to extract data from websites efficiently, processes it as we see fit, and store it in the structure and format we prefer. Zyte (formerly Scrapinghub), a web scraping development and services company, 5 min read How to Check Loading Time of Website using Python In this article, we will discuss how we can check the website's loading time. Do you want to calculate how much time it will take to load your website? Then, what you must need to exactly do is subtract the time obtained passed since the epoch from the time obtained after reading the whole website. 3 min read How to Scrape Multiple Pages of a Website Using Python? Web Scraping is a method of extracting useful data from a website using computer programs without having to manually do it. This data can then be exported and categorically organized for various purposes. Some common places where Web Scraping finds its use are Market research & Analysis Websites 6 min read Web crawling with Python Web crawling is widely used technique to collect data from other websites. It works by visiting web pages, following links and gathering useful information like text, images, or tables. Python has various libraries and frameworks that support web crawling. In this article we will see about web crawl 4 min read Python | Launch a Web Browser using webbrowser module In Python, webbrowser module is a convenient web browser controller. It provides a high-level interface that allows displaying Web-based documents to users. webbrowser can also be used as a CLI tool. It accepts a URL as the argument with the following optional parameters: -n opens the URL in a new 2 min read Extract title from a webpage using Python Prerequisite Implementing Web Scraping in Python with BeautifulSoup, Python Urllib Module, Tools for Web Scraping In this article, we are going to write python scripts to extract the title form the webpage from the given webpage URL. Method 1: bs4 Beautiful Soup(bs4) is a Python library for pulling 3 min read Like