Changes made are not reversing

22 Views Asked by At

I was trying to build a simple program that blocks the websites for a given time. The websites are listed in a txt file that I have locked for security reasons. This was my first time experimenting with filelock systems and the problem is that the execution of the website blockage works perfectly but once the time is over the websites remain locked. I would be really thankful if anyone can let me know what could be the reason that the websites remain locked. I am really not into filelock module so there could be something that I might have done wrong.

Also after I locked the text files, all the text disappeared, is that normal and what would that be for?

`

from datetime import datetime
from filelock import FileLock


date = str(datetime.date(datetime.now()))
year = int(date[:4])
month = int(date[5:7])
day = int(date[8:])

hour = int(input("When do you want to end this trial : "))
minute = input("What minute do you want to end this trial : ")

if minute in 'Qq':
    end_time = datetime(year, month, day, hour)
else:
    end_time = datetime(year, month, day, hour, int(minute))


lock = FileLock('./blocked_sites.txt.lock')

with lock:
    with open('./blocked_sites.txt', mode='r+') as sites:
        sites_to_block = sites.readlines()


hosts_path = r"C:\Windows\System32\drivers\etc\hosts"
redirect = '127.0.0.1'


def block_websites():
    if datetime.now() < end_time:
        print("Block sites")
        with open(hosts_path, 'r+') as hostfile:
            hosts_content = hostfile.read()

            lock.acquire()
            for site in sites_to_block:
                if site not in hosts_content:
                    hostfile.write(redirect + ' ' + site + '\n')

    else:
        print('Unblock sites')
        with open(hosts_path, 'r+') as hostfile:
            lines = hostfile.readlines()
            hostfile.seek(0)
            for line in lines:
                if not any(site in line for site in sites_to_block):
                    hostfile.write(line)
            hostfile.truncate()


if __name__ == '__main__':
    block_websites()

`

1

There are 1 best solutions below

0
NameVergessen On

It is a bit difficult ro reproduce. You probably need a main loop. Something to reexecute your code or wait for the second part of your code which is easier for your code structure.

try:

import time

def block_websites():
    if datetime.now() < end_time:
        print("Block sites")
        with open(hosts_path, 'r+') as hostfile:
            hosts_content = hostfile.read()

            lock.acquire()
            for site in sites_to_block:
                if site not in hosts_content:
                    hostfile.write(redirect + ' ' + site + '\n')
    # add this code snipped
    else:
        return
    while datetime.now() < end_time:
        time.sleep(1.0)

    print('Unblock sites')
    with open(hosts_path, 'r+') as hostfile:
        lines = hostfile.readlines()
        hostfile.seek(0)
        for line in lines:
            if not any(site in line for site in sites_to_block):
                hostfile.write(line)
        hostfile.truncate()

I would recomend using the datetime and date attributes instead of string manipulation.

from datetime import datetime
date = datetime.now().date()
year = date.year
month = date.month
day = date.day

hour = input("At what hour do you want to end this trial : ")
minute = input("What minute do you want to end this trial : ")

if minute in 'Qq':
    end_time = datetime(year, month, day, 0, 0)
else:
    end_time = datetime(year, month, day, int(hour), int(minute))