import tkinter as tk
from tkinter import ttk
import smtplib
import socket
import requests
import datetime
import pyautogui
import threading
import os
import sys
import time
import ctypes
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header
USER32 = ctypes.windll.user32
SW\_SHOW = 5
SW\_HIDE = 0
class EmailChecker:
def \_\_init\_\_(self):
self.sent\_email = False
self.stop\_event = threading.Event()
self.thread = None
self.root = tk.Tk()
self.root.title("Smilinghan-SPCWC")
self.create\_widgets()
def create\_widgets(self):
mainframe = ttk.Frame(self.root, padding="20 10 20 10")
mainframe.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E, tk.S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
email\_entry = ttk.Entry(mainframe, width=30)
email\_entry.grid(column=1, row=0, sticky=(tk.W, tk.E))
email\_label = ttk.Label(mainframe, text="Email:")
email\_label.grid(column=0, row=0, sticky=tk.E)
submit\_button = ttk.Button(mainframe, text="确定", command=lambda: self.submit\_email(email\_entry))
submit\_button.grid(column=2, row=0, sticky=tk.W)
start\_button = ttk.Button(mainframe, text="开始检测", command=self.start\_thread)
start\_button.grid(column=0, row=2, sticky=tk.W)
stop\_button = ttk.Button(mainframe, text="停止检测", command=self.stop\_thread)
stop\_button.grid(column=2, row=2, sticky=tk.E)
output\_text = tk.Text(mainframe, height=10, width=50)
output\_text.grid(column=0, row=3, columnspan=3, sticky=(tk.W, tk.E))
for child in mainframe.winfo\_children():
child.grid\_configure(padx=5, pady=5)
email\_entry.focus()
self.root.bind('<Return>', lambda event: self.submit\_email(email\_entry))
self.output\_text = output\_text
def submit\_email(self, email\_entry):
email = email\_entry.get()
self.output\_text.insert(tk.END, "接收的电子邮件: " + email + "\n")
with open("emails.txt", "w") as f:
f.write(email + "\n")
email\_entry.delete(0, tk.END)
email\_entry.insert(0, "")
def send\_email(self, subject, content, receiver):
sender = 'smilinghan@qq.com'
password = 'password'
with open("emails.txt", "r") as f:
receivers = [f.read().strip()]
message = MIMEMultipart()
message['Subject'] = Header(subject, 'utf-8')
message['From'] = Header(sender)
message['To'] = Header(receiver)
text = MIMEText(content, 'html', 'utf-8')
message.attach(text)
screenshot = pyautogui.screenshot()
if screenshot is not None:
screenshot.save('screenshot.png')
with open('screenshot.png', 'rb') as f:
img = MIMEImage(f.read())
img.add\_header('Content-ID', '<screenshot>')
message.attach(img)
try:
smtpObj = smtplib.SMTP\_SSL('smtp.qq.com', 465)
smtpObj.login(sender, password)
smtpObj.sendmail(sender, receivers, message.as\_string())
self.output\_text.insert(tk.END, "邮件发送成功\n")
self.sent\_email = True
except Exception as e:
self.output\_text.insert(tk.END, "邮件发送失败\n")
self.output\_text.insert(tk.END, str(e) + "\n")
self.sent\_email = False
def check\_system(self, receiver):
try:
while not self.stop\_event.is\_set():
if USER32.GetForegroundWindow() != 0:
hostname = socket.gethostname()
ip\_address = socket.gethostbyname(hostname)
response\_ip = requests.get("https://www.90th.cn/api/ip")
if response\_ip.status\_code == 200:
public\_ip = response\_ip.json()["ip"]
address = response\_ip.json()["address"]
else:
public\_ip = "获取失败"
address = "获取失败"
login\_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
subject = '系统状况报告'
content = f"""
<html>
<body>
<h3>系统状况报告</h3>
<p>主机名: {hostname}</p>
<p>外网IP: {public\_ip}</p>
<p>内网IP: {ip\_address}</p>
<p>归属地: {address}</p>
<p>发送时间: {login\_time}</p>
<p>登录状态: 成功</p>
<p><img src="cid:screenshot"></p>
</body>
</html>
"""
if not self.sent\_email:
self.send\_email(subject, content, receiver)
self.sent\_email = True
time.sleep(5)
else:
time.sleep(1)
self.sent\_email = False
self.output\_text.insert(tk.END, "电脑未唤醒\n")
except Exception as e:
self.output\_text.insert(tk.END, "Error: 无法检测电脑唤醒状态\n")
self.output\_text.insert(tk.END, str(e) + "\n")
self.sent\_email = False
def start\_thread(self):
if self.thread is None or not self.thread.is\_alive():
with open("emails.txt", "r") as f:
receiver = f.read().strip()
self.thread = threading.Thread(target=self.check\_system, args=(receiver,), daemon=True)
self.thread.start()
self.output\_text.insert(tk.END, "开始检测\n")
def stop\_thread(self):
if self.thread is not None and self.thread.is\_alive():
self.stop\_event.set()
self.thread.join(timeout=3)
self.output\_text.insert(tk.END, "停止检测\n")
def run(self):
if getattr(sys, 'frozen', False):
os.chdir(sys.\_MEIPASS)
self.root.protocol("WM\_DELETE\_WINDOW", self.stop\_check)
self.root.mainloop()
def stop\_check(self):
if not self.stop\_event.is\_set():
self.stop\_event.set()
if self.thread is not None and self.thread.is\_alive():
self.thread.join(timeout=3)
self.stop\_thread()
self.root.destroy()
self.root.quit()
for t in threading.enumerate():
if t != threading.current\_thread():
t.join(timeout=3)
if \_\_name\_\_ == '\_\_main\_\_':
checker = EmailChecker()
checker.run()
pip install -r requirements.txt
python email_checker.py
send\_email
方法中提供正确的电子邮件地址和密码。pyautogui
库进行屏幕截图。请确保您已安装该库并具有进行屏幕截图的必要权限。stop\_event
用于在用户单击“停止检测”按钮时停止监控过程。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。