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() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com