Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 날짜
- pandas
- PyQt
- GIT
- python
- 맛집
- 다이어트
- 라즈베리파이
- ASP
- ubuntu
- 리눅스
- Unity
- port
- sqlite
- 유니티
- node.js
- PER
- IOS
- flutter
- javascript
- tensorflow
- Excel
- MySQL
- PyQt5
- mssql
- swift
- urllib
- Linux
- MS-SQL
- 함수
Archives
아미(아름다운미소)
화면보호기 본문
from pynput import keyboard, mouse
from pynput.keyboard import Controller, Key
import threading
import time
from datetime import datetime
import ctypes
import os
# ✅ 로그 저장 기본 디렉토리 (원하는 경로로 변경하세요)
LOG_BASE_DIR = r"D:/logs"
# 설정
IDLE_TIME_LIMIT = 300 # 5분 이상 비활동 시
CHECK_INTERVAL = 10 # 활동 체크 주기
keyboard_controller = Controller()
last_activity_time = time.time()
# 로그 기록 함수
def write_log(message):
now = datetime.now()
timestamp = now.strftime('%Y-%m-%d %H:%M:%S')
year = now.strftime('%Y')
month = now.strftime('%m')
day = now.strftime('%d')
log_dir = os.path.join(LOG_BASE_DIR, year, month)
os.makedirs(log_dir, exist_ok=True) # 폴더 없으면 생성
log_path = os.path.join(log_dir, f"{day}.txt")
log_line = f"[{timestamp}] {message}"
print(log_line)
with open(log_path, "a", encoding="utf-8") as f:
f.write(log_line + "\n")
# 사용자 입력 감지 핸들러
def on_input_activity(event):
global last_activity_time
last_activity_time = time.time()
# 화면보호기 방지를 위한 Shift 입력
def prevent_sleep_with_key():
write_log("🟡 활동 없음: Shift 입력 시뮬레이션")
keyboard_controller.press(Key.shift)
time.sleep(0.1)
keyboard_controller.release(Key.shift)
# 화면 잠금
def lock_workstation():
write_log("🔒 화면 잠금 실행")
ctypes.windll.user32.LockWorkStation()
# 시스템 종료
def shutdown_system():
write_log("⏹️ 시스템 종료 명령 실행")
os.system("shutdown /s /t 0")
# 시스템 재부팅
def reboot_system():
write_log("🔁 시스템 재부팅 명령 실행")
os.system("shutdown /r /t 0")
# 활동 감지 쓰레드
def monitor_idle():
while True:
idle_time = time.time() - last_activity_time
if idle_time >= IDLE_TIME_LIMIT:
prevent_sleep_with_key()
time.sleep(CHECK_INTERVAL)
# 시간 기반 이벤트 실행
def scheduled_actions():
triggered = set()
while True:
now = datetime.now()
current_time = now.strftime('%H:%M')
weekday = now.weekday() # 0:월 ~ 4:금
# 오전 11시 화면 잠금
if current_time == "11:00" and "lock" not in triggered:
lock_workstation()
triggered.add("lock")
# 오후 5시 종료/재부팅
if current_time == "17:00" and "shutdown" not in triggered:
if weekday in [0, 1, 2, 3]: # 월~목
write_log("🔁 월~목: 시스템 재부팅 예정")
reboot_system()
elif weekday == 4: # 금
write_log("⏹️ 금요일: 시스템 종료 예정")
shutdown_system()
triggered.add("shutdown")
if current_time == "00:00":
triggered.clear()
time.sleep(5)
# 입력 리스너 시작
keyboard.Listener(on_press=on_input_activity).start()
mouse.Listener(
on_move=on_input_activity,
on_click=on_input_activity,
on_scroll=on_input_activity
).start()
# 쓰레드 실행
threading.Thread(target=monitor_idle, daemon=True).start()
threading.Thread(target=scheduled_actions, daemon=True).start()
write_log("✅ 활동 감지 + 자동 잠금/재부팅/종료 프로그램 시작됨 (Ctrl+C로 종료 가능)")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
write_log("🟥 프로그램 수동 종료됨")
pyinstaller --noconsole --onefile idle_guard.py
'랭귀지 > pandas' 카테고리의 다른 글
np.select 멀티프로세싱 적용 (0) | 2025.06.12 |
---|---|
Type 변경 (0) | 2025.06.09 |
join (0) | 2025.06.02 |
merge (0) | 2025.05.28 |
object 타입 컬럼을 모두 문자열(str)로 변환 (0) | 2025.05.26 |
Comments