时间:2020-10-11 10:45:41 | 栏目:Python代码 | 点击:次
Python 通过微信邮件实现电脑关机,供大家参考,具体内容如下
通过手机微信发送QQ邮件给sina邮箱,然后利用python的pop3定时检查sina邮箱的邮件主题以及邮件来源,并在电脑执行相应的命令行实现关机。
Email_test【V1.0】
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()