标签:关闭 头信息 set split poplib size 列表 sub col
1 import poplib 2 import email 3 from email.header import decode_header 4 5 #定义获取邮件标题的函数 6 def get_email_subject(username,password): 7 8 #实例化邮件对象 9 read = poplib.POP3("pop.163.com",timeout=3600) 10 11 #登录 12 read.user(username) #用户名 13 read.pass_(password) #客户端授权码 14 15 #获取邮件总数,邮件大小 16 total_num,total_size = read.stat() 17 18 #获取最新的一封邮件head 19 top_email = read.top(total_num,0) #total_num 代表获取第几个邮件,0代表邮件头后面第几行 20 21 #将邮件头信息放在temp列表中 22 temp = [] 23 for i in top_emain[1]: 24 temp.append(i) 25 26 #将邮件头变成字符串 27 email_str = ‘\n‘.join(temp) 28 29 30 #将字符串解析为Message类型 31 message = email.message_from_string(email_str) 32 33 #拿到邮件主题 34 subject_str = message[‘subject‘] 35 36 #解码得到主题 37 subject = decode_header(subject_str) 38 content = subject[0][0] 39 enc_type = subject[0][1] #类型 40 41 if enc_type: 42 subject_decode = content.decode(enc_type) 43 else: 44 subject_decode = content 45 46 #返回主题,邮件对象,邮件总数 47 return subject_decode, read, total_num
1 import os 2 import time 3 from test01 import get_email_subject #导入函数 4 5 command_set = { 6 "重启":"shutdown -r -t ", 7 "关机":"shutdown -s -t ", 8 } 9 10 11 username = "XXX" #用户名 12 userpwd = "XXX" #客户端授权码 13 control_pwd = "XXX" #身份识别码 14 15 16 while True: 17 18 #调用函数 19 subject_or,read,total_num = get_email_subject(email_addr,email_password) 20 subject = subject_or.replace(",",",") #逗号替换 21 subject_list = subject.split(",")#转化成列表 22 23 if len(subject_list) >= 3:#判断列表长度 24 25 #拿到命令、时间、身份验证码 26 command,execute_time,password = subject_list[:3] 27 28 if execute_time.isdecimal() and int(execute_time) < 60: 29 execute_time = "60" 30 31 #命令拼接 32 execute_command = command_set[command] + execute_time 33 34 read.dele(total_num) #删除最新一封邮件 35 read.quit() #关闭邮件 36 37 if control_pwd == password: #身份验证 38 os.system(execute_command) #执行命令 39 break 40 else: 41 time.sleep(2)
标签:关闭 头信息 set split poplib size 列表 sub col
原文地址:https://www.cnblogs.com/yanhonghong/p/11664483.html