使用python定时自动截屏,发邮件通知

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

1. 截屏,并保存图片

1
2
3
4
5
6
7
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. 定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.发邮件,将图片作为附件

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
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 = '[email protected]'
receiver = '[email protected]'
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()

完整代码

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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 = '[email protected]'
receiver = '[email protected]'
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
里,这样每次开机就能直接启动了。