Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cd190f0

Browse files
added site blocker scripts
1 parent 2a160d2 commit cd190f0

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎Site-blocker/Readme.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Website Blocker
2+
3+
## Description
4+
This is a script that aims to implement a website blocking utility for Windows-based systems. It makes use of the computer's hosts files and runs it as a background process, preventing access to the sites entered by the user in array format.
5+
6+
## Third-party libraries required:
7+
The project requires Python's datetime library only
8+
9+
## Importing the Libraries:
10+
Open Command Prompt on your computer and type the following:
11+
On the script's console, type:
12+
`import time
13+
`from datetime import datetime as dt`
14+
15+
## Running the Script:
16+
After opening the script in your Python IDE, execute the code so that you get the console output window. Open your browser and try to visit the websites you blocked. When the script runs successfully, you will see `This site can't be reached` error on the browser.
17+
18+
**Note:**
19+
> In some systems, access to the computers's hosts files maybe denied by default to prevent malware attacks. So the script while executing may show an error while modifying the hosts files.
20+
`Please visit [here](https://www.technipages.com/windows-access-denied-when-modifying-hosts-or-lmhosts-file) for a brief readup on how to solve the issue.`
21+
22+
## Output:
23+
24+
#### This is how the browser acts when you try to visit the website that you blocked:
25+
<img src="https://xtendedview.com/wp-content/uploads/2020/03/This-Site-Cant-be-Reached.jpg"><br>
26+
<p><em>The acess will be denied to all the mentioned sites as per you changes the list </em></p>

‎Site-blocker/web-blocker.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import time
2+
from datetime import datetime as dt
3+
4+
#Windows host file path
5+
hostsPath=r"C:\Windows\System32\drivers\etc\hosts"
6+
redirect="127.0.0.1"
7+
8+
#Add the website you want to block, in this list
9+
websites=["www.amazon.in", "www.youtube.com","youtube.com", "www.facebook.com", "facebook.com" ]
10+
while True:
11+
#Duration during which, website blocker will work
12+
if dt(dt.now().year,dt.now().month,dt.now().day,9) < dt.now() < dt(dt.now().year,dt.now().month,dt.now().day,18):
13+
print ("Access denied to Website")
14+
with open(hostsPath,'r+') as file:
15+
content = file.read()
16+
for site in websites:
17+
if site in content:
18+
pass
19+
else:
20+
file.write(redirect+" "+site+"\n")
21+
else:
22+
with open(hostsPath,'r+') as file:
23+
content = file.readlines()
24+
file.seek(0)
25+
for line in content:
26+
if not any(site in line for site in websites):
27+
file.write(line)
28+
file.truncate()
29+
print ("Allowed access!")
30+
time.sleep(5)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /