温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python通过微信发送邮件实现电脑关机

发布时间:2020-09-09 12:00:53 来源:脚本之家 阅读:148 作者:shhchen 栏目:开发技术

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI