使用python检测用户登陆状态,pythontuple,win32ts.WT
使用python检测用户登陆状态,pythontuple,win32ts.WT
win32ts.WTSRegisterSessionNotification监控计算机状态改变
https://msdn.microsoft.com/en-us/library/aa383841(v=vs.85).aspx
用户状态改变后,执行函数myststa(event),其中event代表用户状态信息,具体值查
看https://msdn.microsoft.com/en-us/library/aa383828(v=vs.85).aspx
代码:
import win32conimport win32guiimport win32tsWM_WTSSESSION_CHANGE = 0x2B1class WTSMonitor(): className = "WTSMonitor" wndName = "WTS Event Monitor" def __init__(self): wc = win32gui.WNDCLASS() wc.hInstance = hInst = win32gui.GetModuleHandle(None) wc.lpszClassName = self.className wc.lpfnWndProc = self.WndProc self.classAtom = win32gui.RegisterClass(wc) style = 0 self.hWnd = win32gui.CreateWindow(self.classAtom, self.wndName, style, 0, 0, win32con.CW_USEDEFAULT, win32con.CW_USEDEFAULT, 0, 0, hInst, None) win32gui.UpdateWindow(self.hWnd) win32ts.WTSRegisterSessionNotification(self.hWnd, win32ts.NOTIFY_FOR_ALL_SESSIONS) def start(self): win32gui.PumpMessages() def stop(self): win32gui.PostQuitMessage(0) def WndProc(self, hWnd, message, wParam, lParam): if message == WM_WTSSESSION_CHANGE: self.OnSession(wParam, lParam) def OnSession(self, event, sessionID): print(event) myststa(event)if __name__ == ‘__main__‘: m = WTSMonitor() m.start()
使用python检测用户登陆状态
评论关闭