刚刚完成一门课程的网站拦截器的编写,并且遇到了一个网站(特别是Gmail)的问题。无论我输入website_list的URL是什么,我似乎都无法阻止它。
我尝试清除cookie并重新启动计算机,该脚本在其他网站上也能正常工作。我在某处读过有关DNS缓存刷新的信息,但不确定如何执行此操作。
任何建议都会很棒。
脚本:
import time
from datetime import datetime as dt
hosts_path=r"C:\\Windows\System32\drivers\etc\hosts"
redirect="127.0.0.1"
website_list=["www.facebook.com", "facebook.com", "www.gmail.com",
"www.mail.google.com", "mail.google.com",
"https://mail.google.com/mail/u/0/"]
while True:
if(dt(dt.now().year,dt.now().month,dt.now().day,12)
< dt.now() < dt(dt.now().year,dt.now().month,dt.now().day,22)):
print("Working Hours...")
with open(hosts_path, 'r+') as file:
content=file.read()
for website in website_list:
if website in content:
pass
else:
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)
file.truncate()
print("Fun Hours...")
time.sleep(5)