Project for Website Blocker

Hello Everyone, Can anyone tell me below code is optimized or not? I am working on a website blocker python project and looking to source code. I have found this source code from the programming community and want to know your expert view, Is it right to optimize code as size or speed wise?

import time
from datetime import datetime as dt

sites_to_block = [”,

Linux_host = “/etc/hosts”
Window_host = r"C:\Windows\System32\drivers\etc\hosts"
default_hoster = Linux_host
redirect = “”

def block_websites(start_hour, end_hour):
while True:
if (
dt(,,, start_hour)
< dt(,,, end_hour)
print(“Do the work …”)
with open(default_hoster, “r+”) as hostfile:
hosts =
for site in sites_to_block:
if site not in hosts:
hostfile.write(redirect + " " + site + “\n”)
with open(default_hoster, “r+”) as hostfile:
hosts = hostfile.readlines()
for host in hosts:
if not any(site in host for site in sites_to_block):
print(“Good Time”)

if name == “main”:
block_websites(9, 21)

Hi @ankitdixit

These forums are about writing apps in React Native using Expo. This is not the right place to ask your question.

Hey @wodin Thanks, Can you suggest me a better community which is related to python?

You could perhaps try