Lock windows workstation using Python - python

Is there a way to lock the PC from a Python script on Windows?
I do not want to implement some kind of locking on my own - I'd like to use the same lock screen that's also used when the user presses WIN+L or locks the machine via the start menu.

This can be done with the LockWorkStation() function from user32.dll:
This function has the same result as pressing Ctrl+Alt+Del and clicking Lock Workstation.
In Python it can be called using using the ctypes/windll FFI from the Python stdlib:
import ctypes
ctypes.windll.user32.LockWorkStation()

A good solution that makes us avoid using Libraries/DLL files is to use the command prompet/ power shell.
try running this command in your cmd rundll32.exe user32.dll, LockWorkStation....The PC is Locked!!
so we can use subprocess to run this command like this:
import subprocess
cmd='rundll32.exe user32.dll, LockWorkStation'
subprocess.call(cmd)

One more solution :
Run command to install the necessary package
pip install pyautogui
Run the below code
import pyautogui
from time import sleep
pyautogui.hotkey('win', 'r')
pyautogui.typewrite("cmd\n")
sleep(0.500)
pyautogui.typewrite("rundll32.exe user32.dll, LockWorkStation\n")

Related

Is there a way to check what you are running a python program in?

I want to create a script for just Terminal and IDLE, but I don't know how. Using if 'idlelib' in sys.modules: works for seeing if it is running in IDLE, but is there a way to use the same code to find if it is in Terminal by replacing 'idlelib'?
You can try using psutil and os
import psutil
import os
if psutil.Process(os.getpid()).parent().name() in ["cmd.exe","bash"]:
print("in cmd")
Using idle it returned 'pythonw.exe' which shows this works.

How to make ubuntu OS to get sleep using python?

using python coding i want to make make the system to get sleep state , is that possible ?
i need the solution in Ubuntu , i can find that for windows but for Ubuntu is their any solution ?
Using os.system you can call terminal commands.
You can for instance use systemctl hibernate to get Ubuntu to sleep.
import os
os.system("systemctl hibernate")
Note: you might need to run your python file using sudo.

Python automate open application

Please give me idea regarding how to tackle this problem. I am not able to find any resource regarding this. Your help will be immensely valuable. So we have one limited license software. And want to reiterate the python invoking the application. If the application gives the error that licence is not available it should close the application and wait for sometime say 1 min and again invoke the process, it should do so endlessly until a licence is available and the application is finally open.
I am able to open the application using
Import os
os.startfile('application executable')
After this I want the application to know if there is an error window popping , it should close the window and wait for sometime and again open the application
os.startfile returns as soon as the associated application is launch so use Popen instead.
As you are using windows use these steps.
To Open a Shortcut using Popen on Windows first install pywin32
Step one:
python -m pip install pywin32
Step two:
Navigate to your python Scrips folder something like
C:\Users\Name\AppData\Local\Programs\Python\Python38-32\Scripts then type the command.
pywin32_postinstall.py -install
Then the code to use Popen is.
import subprocess
import win32com.client, win32api
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(r'path to shortcut')
long_path = shortcut.Targetpath
p = subprocess.Popen(long_path)
p.wait()

How to run an autohotkey script in Python (2.7)

I'm trying to run an autohotkey (ahk) script in Python 2.7 but nothing seems to work. All online sources I've found are either outdated or overly complicated.
Has anyone found a way of doing this? I just want to run a couple of simple scripts that activates windows and opens applications. E.g:
IfWinExist, Command Prompt - python ...
WinActivate
Update:
I've tried downloading pyahk:
ahk.start() # Ititializes a new script thread
ahk.ready() # Waits until status is True
ahk.execute(mw._['cwd']+"winActivate_cmd.ahk") # Activate cmd window
error: can't load autohotkey.dll
as well as trying this:
import win32com.client # Import library / module
dll = win32com.client.Dispatch("AutoHotkey.Script") #Creating DLL object?
dll.ahktextdll() #no idea what this is doing...
dll.ahkExec("WinActivate, Command Prompt - python")
pwintypes.com_error invalid class string
It seems like you should be able to just launch autohotkey with the script as a parameter using subprocess:
subprocess.call(["path/to/ahk.exe", "script.ahk"])
You'd have to check the autohotkey docs but this seems like it ought to work.

How can I make a program that can run cmd commands in python3.x

I trying to make a program that can access other computers in the same network by using the cmd command. I have an administrator password that work in all computers in our network. But I don't know how to make a program that includes cmd commands? Which module should I use or are there other ways to do that?
Use python's os module.
import os
os.system('dir')
os.system('ping 127.0.0.1')

Categories

Resources