ПРОБЛЕМА:
Итак, я хочу реализовать функцию, которая автоматически снимает ограничение на Youtube через определенный период времени, скажем, после 21:00. Я подготовил для этой цели «if-else» (часть с комментариями в моем коде), но не использовал его, поскольку не знаю, как это сделать. Я думал об использовании таких модулей, как «datetime», но не знал, как именно это реализовать. Вот код ниже:
Код: Выделить всё
#Using task Scheduler to automatically enable the programme on the startup of my computer
#import datetime
sites_to_block = ["www.youtube.com"]
hosts_path = r"C:\Windows\System32\drivers\etc\hosts"
redirect = "127.0.0.1"
def block():
#if datetime.now() < end_time:
print("block sites")
with open(hosts_path , 'r+') as hostsfile:
hosts_content = hostsfile.read()
for site in sites_to_block:
if site not in hosts_content:
hostsfile.write(redirect + " " + site + "\n")
#The following will be the code after the blocktime specified by the user is over.
'''else:
print('unblock sites')
with open(hosts_path,'r+') as hostsfile:
lines = hostsfile.readlines()
hostsfile.seek(0)
for line in lines:
if not any(site in line for site in sites_to_block):
hostsfile.write(line)
hostsfile.truncate()
'''
if __name__ == "__main__":
block()
Спасибо, мне очень приятно, что вы это прочитали
Подробнее здесь: https://stackoverflow.com/questions/686 ... be-blocker
Мобильная версия