Website Blocker Using Python - GeeksforGeeks

16 Jul, 2020
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/hostsHost file in Windows:C:\Windows\System32\drivers\etcWorking 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:
Every system have host file whether it is Mac, Windows or Linux.Host file in Mac and Linux :/etc/hostsHost file in Windows:C:\Windows\System32\drivers\etc
/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:
Host file in Mac:
# Run this script as root  import timefrom datetime import datetime as dt  # change hosts path according to your OShosts_path = "/etc/hosts"# localhost's IPredirect = "127.0.0.1"  # websites That you want to blockwebsite_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 -eYour 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.
Write following command in terminal:sudo crontab -eYour terminal should look like this:
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.
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.
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.
Akanksha_Rai
Python-projects
python-utility
GBlog
Python
Technical Scripter
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
GET and POST requests using Python
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Types of Software Testing
Working with csv files in Python
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe