Cerca qui le cose strambe

2025-04-09

preventing rdp session from being locked with pyautogui

 
I have to work on 2 simultaneous rdp connections on two screens 
and every 5/10 minutes or so one of them got locked (you know: security policies...)

so I ended up with this workaround: a simple infinite-loop python script that 
I can launch in a local cmd shell: 
every minute it simulates a click and an Esc key in both rdp window (and this, in my specific case, cannot cause any harm, but beware: YMMV)


import pyautogui, random, sys  
from tkinter import Tk  
from time import sleep
  
pyautogui.PAUSE = 0.01  
num_times = 0  
while True:  
    num_times += 1  
    print("loop number %d" %  num_times)  
sleep(1) print(".") sleep(1) print(".")
z1, z2 = pyautogui.position() pyautogui.moveTo(-700, 300, duration=0.1) pyautogui.click() sleep((0.4 + random.random()) / 4) pyautogui.hotkey("esc") pyautogui.moveTo(z1, z2, duration=0.1) pyautogui.click() sss = 48.4 + 6.01 * random.random() print("sleeping for %d s " % int(sss)) sleep(sss) print("this statement just marks the script end, but it won't be ever executed")

UPDATED on 2026-03-25
there is a simpler solution using
https://pypi.org/project/wakepy/
=>
from wakepy import keep
from time import sleep

@keep.presenting
def long_running_task():
    while True:
        sleep(60)
        print(datetime.now())

long_running_task()


No comments: