Python 通过微信邮件实现电脑关机,供大家参考,具体内容如下
通过手机微信发送QQ邮件给sina邮箱,然后利用python的pop3定时检查sina邮箱的邮件主题以及邮件来源,并在电脑执行相应的命令行实现关机。
Email_test【V1.0】
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
import poplib
import os
import time
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
#编码转换函数
def decode_str(s):
value, charset = decode_header(s)[ 0 ]
if charset:
value = value.decode(charset)
return value
#获取email主题
def get_Subject(msg):
#提取Subject信息
Subject = msg.get( 'Subject' )
#编码转换
Subject = decode_str(Subject)
return Subject
def judge(Subject, e_addr):
if (Subject = = '关机' and e_addr = = '532101629@qq.com' ):
return 1
else :
return 0
#检索邮件主题
def Check_Subject(host, user, password):
result = 0
try :
pop_connect = poplib.POP3(host = host, timeout = 3 )
print (pop_connect.getwelcome())
pop_connect.user(user)
pop_connect.pass_(password)
print ( 'Messages: %s. Size: %s' % pop_connect.stat())
#服务器返回信息,消息列表,返回信息的大小。
number = len (pop_connect. list ()[ 1 ])
print ( '消息列表长度:' , number)
#检索所有邮件
for index in range ( 1 , number + 1 ):
#获取第一封邮件信息
msglines = pop_connect.retr(index)[ 1 ]
# 可以获得整个邮件的原始文本(重新排版后的):
str = b '\r\n'
msg_content = str .join(msglines).decode( 'utf-8' )
print ( '\n' , msg_content)
#将原始邮件转换为email实例:
msg = Parser().parsestr(msg_content)
# 获取email主题
Subject = get_Subject(msg)
print (Subject)
# 获取email地址
email_addr = parseaddr(msg.get( 'From' ))[ 1 ]
#信息判断
result = judge(Subject, email_addr)
print (result)
#根据判断结果,执行操作
if result = = 1 :
pop_connect.dele(index)
break
# 登出email
pop_connect.quit()
return result
except Exception as e:
print ( 'login fail! ' + str (e))
quit()
def main():
host = 'pop.sina.com'
user = '********@sina.com'
password = '********'
while 1 :
result = Check_Subject(host, user, password)
if result = = 1 :
cmd = 'cmd /k shutdown -l'
os.system(cmd)
break
time.sleep( 60 ) # 两次检索邮件的时间间隔60s
main()
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/shhchen/article/details/52451040