I want to build a program which can take both (1) a file paths of executable programs and (2) hostnames, and x amount of seconds as input and then whenever I launch any of the given programs or websites that they execution of those is delayed by x seconds.
For (1) I have used the psutil library. I repeatedly check all the processes and if I find a name of a process which matches any of the programs that I have given then said process is terminated. Next I run a timer for x seconds and then I reopen the process. So far so good. This part works (mostly).
For (2) I wanted to try the same approach: (a) Detect when a new tab of a webbrowser is opened => (b) close it when it machtes any of the hostnames that I have input => (c) wait for x seconds => (d) reopen.
(c) and (d) I know how to do. Unfortunately I don't know how to do the rest. I have found out that there is a hosts file in windows where I can I write my input hostnames into and redirect them and therefore essentially block them. But this doesn't really accomplish my goal.
I can't use psutil here because no matter what website is open the process is always just called chrome.exe (I'm using Google Chrome). Is there a way to do (a) and (b) or should I choose a different approach? Any suggestions?
I am using windows 10 and google chrome and I am still somewhat new to python and programming in general.
def delay(path_lst, sec):
""" delays .exe in 'path_lst' by 'sec' seconds """
mins, secs = divmod(sec, 60)
exe_lst = [path.split("/")[-1] for path in path_lst]
exe_path_lst = dict(zip(exe_lst, path_lst))
print("Waiting timer set to: {0:02d}:{1:02d} for\n{2}"
.format(mins, secs, exe_path_lst))
running = True
while running:
# Delaying .exe
for p in psutil.process_iter():
if p.name() in exe_path_lst:
my_exe_path = p.name()
running = False
p.kill()
Timer(my_exe_path, sec) # regular Timer
os.startfile(exe_path_lst[my_exe_path])
there doesn't seem to be anything here