tlnshuju

Python3.4 远程操控电脑(开关机)

2017-06-21 09:27  tlnshuju  阅读(471)  评论(0编辑  收藏  举报
import poplib
import sys
import smtplib
from email.mime.text import MIMEText
import os
from email.header import decode_header
import email
import time
def check_email():
    try:
        p = poplib.POP3(\'pop.163.com\')
        p.user(\'*****@163.com\')
        p.pass_(\'*******\')
        ret = p.stat()
    except:
        print(\'Login failed!\')
        sys.exit(1)
    str = p.top(ret[0], 0)
    strlist = []
    for x in str[1]:
            try:
                strlist.append(x.decode())
            except:
                try:
                    strlist.append(x.decode(\'gbk\'))
                except:
                    strlist.append((x.decode(\'big5\')))
    mm = email.message_from_string(\'\n\'.join(strlist))
    sub = decode_header(mm[\'subject\'])
    if sub[0][1]:
        submsg = sub[0][0].decode(sub[0][1])
    else:
        submsg = sub[0][0]
    if submsg.strip() == \'关机\':
        return 0
    elif submsg.strip() == \'重新启动\':
        return 1
    p.quit()
def send_email():
    user = \'*******@qq.com\'
    pwd = \'**********\'
    to = [\'*****@163.com\', \'*****@139.com\']     #139邮件会有短信提醒,让我知道是否成功
    msg = MIMEText(\'\')
    msg[\'Subject\'] = \'已收到命令!\'
    msg[\'From\'] = user
    msg[\'To\'] = \',\'.join(to)
    s = smtplib.SMTP(\'smtp.qq.com\')
    s.login(user, pwd)
    s.sendmail(user, to, msg.as_string())
    s.close()

if __name__ == \'__main__\':
    while True:
        time.sleep(20)
        if check_email() == 0:
            send_email()
            os.system(\'shutdown -s -t 1\')    #关机
            break
        if check_email() == 1:
            send_email()
            os.system(\'shutdown -r\')      #重新启动
            break
測试成功

其他命令自行加入,这里仅仅作 关机和重新启动. 脑洞大开!

转载请注明作者与出处:http://blog.csdn.net/u013511642   王小涛_同學


分类:

技术点:

相关文章:

  • 2021-12-15
  • 2021-09-01
  • 2021-12-25
  • 2021-12-31
  • 2021-12-31
  • 2021-11-30
  • 2021-12-15
猜你喜欢
  • 2021-06-06
  • 2021-04-07
  • 2021-05-29
  • 2021-12-16
  • 2021-12-31
  • 2021-11-16
  • 2021-10-31
相关资源
相似解决方案