简体   繁体   中英

Unlock Computer Screen Using Python

I had written some automation code for our site in which used sikuli for image automation. But sometimes I found that my computer screen locked in between, and that's why many of the test cases failed. So I want to write some python code to keep the screen unlocked.

Here is the code patch which I wrote for locking the screen and now I am searching for how to unlock the screen:

import os

winpath = os.environ["windir"]
os.system(winpath + r'\system32\rundll32 user32.dll, LockWorkStation')

Note: I have already adjusted all the monitor settings which cause screen locking.

To work properly Sikuli need UI. This means the user should be logged in. Below are the steps on how to achieve that:

Use Autologon

  1. Download and use Sysinternals Autologon

You should redirect the Console in order to have UI anytime

  1. Create New Task in Task Scheduler
  2. Next in General tab check " Run with highest privileges "
  3. Next in Triggers tab add New trigger
  4. Next in New Trigger dialog select " On disconnect from user session " from "Begin the task" dropdown the press OK with default settings or with the users you will login usually
  5. Create a switch-ui.bat file with the following content in a folder switch-ui on Desktop

echo ################################################### >> %USERPROFILE%\Desktop\rdc_switch_log.log 
date /t >> %USERPROFILE%\Desktop\rdc_switch_log.log time /t >> %USERPROFILE%\Desktop\rdc_switch_log.log 
timeout 3 
query session >> %USERPROFILE%\Desktop\rdc_switch_log.log 
query session > %localappdata%\log.txt

findstr "rdp" %localappdata%\log.txt| findstr "Active" 

if %errorlevel%==0 ( 
    echo You are in RDP session. Do nothing! >>%localappdata%\log.txt 
    ) 
else ( 
    tscon 1 /dest:console 
    tscon 2 /dest:console 
    tscon 3 /dest:console 
    tscon 4 /dest:console 
    )
  1. Next in Actions tab Action
  2. Next in New Action dialog select "Start a program" from Action dropdown
  3. In Settings Program/script add the full path to your file "C:\\Users\\%yourUsers%\\Desktop\\switch-ui\\switch-ui.bat"
  4. In Start in (optional) add path to your folder with your script "C:\\Users\\%yourUsers%\\Desktop\\switch-ui"
  5. Press OK and finish the Task

Important: In order to test that it works.

  1. Get a video recorder
  2. Login on the machine from Remote Desktop Connection (RDC)
  3. Start video
  4. Close RDC
  5. Wait 2 mins
  6. Connect Again
  7. Stop Video
  8. Watch the video

You should have a black video recorded only when you Close RDC and when you connect back with RDC for a few seconds it depends on how fast is your machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM