Skip to content

Commit e5700bc

Browse files
committedOct 2, 2018
Added Website Blocker
1 parent de4b70a commit e5700bc

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
 

‎Website-Blocker/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Website Blocker using Python
2+
This is a program which blocks certain distracting website like Facebook, Youtube etc during your work hours.
3+
4+
## Libraby Used
5+
time (datetime is imported from python)
6+
7+
## Host Files
8+
Host is an operating system file which maps hostnames to IP addresses.
9+
Using python file handling manipulation I have changed hostnames on the hosts files for a certain interval of day when I'm working and need no distraction and deleted it then after when the time is over.
10+
11+
## Location of host file
12+
### Host file on Mac and Linux :
13+
$ /etc/hosts
14+
15+
### Host file on windows :
16+
$ C:\Windows\System32\drivers\etc
17+
18+
## Note
19+
* 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.
20+
* For scheduling above script in Linux you have to open crontab in your terminal as a root.(use sudo command)

‎Website-Blocker/hosts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
127.0.0.1 localhost
2+
127.0.1.1 hastagab-Latitude-6430U
3+
4+
# The following lines are desirable for IPv6 capable hosts
5+
::1 ip6-localhost ip6-loopback
6+
fe00::0 ip6-localnet
7+
ff00::0 ip6-mcastprefix
8+
ff02::1 ip6-allnodes
9+
ff02::2 ip6-allrouters

‎Website-Blocker/website_blocker.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import time
2+
from datetime import datetime as dt
3+
4+
host_temp = "hosts" #host file copied to the directory for ease.
5+
host_path = r"/etc/hosts" #original host file address in linux
6+
redirect = "127.0.0.1"
7+
website_list = ["www.facebook.com", "facebook.com"] #You can add your own list of websites
8+
9+
while True:
10+
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): #You can choose your own working time period
11+
print("working hours...")
12+
with open(host_path,'r+') as file:
13+
content=file.read()
14+
for website in website_list:
15+
if website in content:
16+
pass
17+
else:
18+
file.write(redirect+" "+ website+"\n")
19+
else:
20+
with open(host_path,'r+') as file:
21+
content=file.readlines()
22+
file.seek(0)
23+
for line in content:
24+
if not any(website in line for website in website_list):
25+
file.write(line)
26+
file.truncate()
27+
print("fun hours...")
28+
time.sleep(10)

0 commit comments

Comments
 (0)