美文网首页
Python 定时自动截屏,发邮件通知

Python 定时自动截屏,发邮件通知

作者: 洗洗睡吧i | 来源:发表于2019-01-20 14:45 被阅读0次

前一段做试验,需要远程监控现场的电脑;
写了一个定时截屏并把图片发送至邮箱的脚本,这样微信就可以得到通知。

1. 截屏,并保存图片

from PIL import ImageGrab
import time

time_now = time.strftime('%Y%m%d-%H%M%S')
pic = ImageGrab.grab()
pic_name = time_now+'.jpg'
pic.save(pic_name)

2. 定时任务

import threading

def func():
    t = time.localtime(time.time())
    min, hour, wkday = t.tm_min, t.tm_hour, t.tm_wday
    # --------add tasks here--------------------------
    if min in [0, 15, 30, 45]:
        print('tasks')   
    # --------tasks end----------------------------

    global timer
    timer = threading.Timer(60, func, [])
    timer.start()


timer = threading.Timer(1, func, [])
timer.start()

3.发邮件,将图片作为附件

from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL

from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart

def send_mail(mail_title='', mail_content='', pic_name=''):
    sender = 'xx@qq.com'
    receiver = 'xx@qq.com'
    pwd = 'xxxxxxxxxxxx'                # qq邮箱授权码

    smtp = SMTP_SSL('smtp.qq.com')      # ssl登录
    smtp.login(sender, pwd)

    msg = MIMEMultipart()
    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["from"] = sender
    msg["to"] = receiver

    puretext = MIMEText('content: '+mail_content)
    msg.attach(puretext)
    
    jpgpart = MIMEApplication(open(pic_name, 'rb').read())
    jpgpart.add_header('Content-Disposition', 'attachment', filename=pic_name)
    msg.attach(jpgpart)
    
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

完整代码

from datetime import datetime
import time
from PIL import ImageGrab
import os
import threading


def send_mail(mail_title='', mail_content='', pic_name=''):
    from email.mime.text import MIMEText
    from email.header import Header
    from smtplib import SMTP_SSL

    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart

    sender = 'xx@qq.com'
    receiver = 'xx@qq.com'
    pwd = 'xxxxxxxxxxxx'                # qq邮箱授权码

    smtp = SMTP_SSL('smtp.qq.com')      # ssl登录
    smtp.login(sender, pwd)

    msg = MIMEMultipart()
    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["from"] = sender
    msg["to"] = receiver

    puretext = MIMEText('content: '+mail_content)
    msg.attach(puretext)

    jpgpart = MIMEApplication(open(pic_name, 'rb').read())
    jpgpart.add_header('Content-Disposition', 'attachment', filename=pic_name)
    msg.attach(jpgpart)
    
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()
        

def func():
    t = time.localtime(time.time())
    min, hour, wkday = t.tm_min, t.tm_hour, t.tm_wday
    # --------add tasks here--------------------------
    if min in [0, 15, 30, 45]:
        time_now = time.strftime('%Y%m%d-%H%M%S')
        pic = ImageGrab.grab()
        pic_name = time_now+'.jpg'
        pic.save(pic_name)

        title = 'mgso4 test monitor ' + time_now
        send_mail(title, title, pic_name)
        # os.remove(pic_name)
    # --------tasks end----------------------------

    global timer
    timer = threading.Timer(60, func, [])
    timer.start()


if __name__ == "__main__":
    timer = threading.Timer(1, func, [])
    timer.start()

PS1:去掉python程序的命令行窗口

把 *.py 文件后缀改成 *.pyw ,即可去掉运行时出现的cmd窗口,直接后台运行。

PS2:加入开机启动(Win10)

将 *.pyw 文件做一个快捷方式,放到开始菜单的启动文件夹C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp里,这样每次开机就能直接启动了。

相关文章

网友评论

      本文标题:Python 定时自动截屏,发邮件通知

      本文链接:https://www.haomeiwen.com/subject/qtpxjqtx.html