加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python3.4+pyqt4邮件关机

发布时间:2020-12-17 17:21:40 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 # -*- coding: utf-8 -*-"""Module implementing Dialog."""import pickleimport smtplib import email.mime.multipart import email.mime.text impor

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

# -*- coding: utf-8 -*-

"""
Module implementing Dialog.
"""
import pickle

import smtplib  
import email.mime.multipart  
import email.mime.text  

import os,sys,string,email
import poplib
import winsound
import time


#from PyQt4.QtCore import pyqtSlot,QMessageBox
#from PyQt4.QtGui import QDialog

from PyQt4.QtGui import *
from PyQt4.QtCore import *

from Ui_shutdown import Ui_Dialog


class Dialog(QDialog,Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self,parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget (QWidget)
        """
        super().__init__(parent)
        self.setupUi(self)
        
        
        self.email=''
        self.password=''
        self.reemail=''
        self.user={} 
        self.subject={}
        self.load_data()
        self.lineEmail.setText(self.user['email'])
        self.linePw.setText(self.user['password'])
        self.lineReEmail.setText(self.user['reemail'])
    @pyqtSlot()
    def on_pushButtonOK_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        #pass
        #raise NotImplementedError
        self.email=self.lineEmail.text()
        self.password=self.linePw.text()
        self.reemail=self.lineReEmail.text()
        self.save_data()
        winsound.PlaySound('OK',winsound.MB_OK)
        QMessageBox.information(self,"OK",'设置成功!')
        #self.shutdowndown()
        while True:
            self.shutdowndown()
            time.sleep(60)
        

        
     
    def save_data(self): 
        '''''
        try:
            os.remove( 'user.pickle' )
        except  WindowsError:
            pass
            '''
        self.user['email']=self.email 
        self.user['password']=self.password  
        self.user['reemail']=self.reemail  
        #with保证自动关闭文件  
        #设置文件模式为'wb'来以二进制写模式打开文件  
        with open('user.pickle','wb') as f:  
            #dump()函数接受一个可序列化的Python数据结构  
            pickle.dump(self.user,f)  
    def load_data(self):
        with open('user.pickle','rb') as f:  
            self.user=pickle.load(f)  
            #user变量是一个字典       
    def accept_mail(self):
        self.load_data()
        host = "pop.163.com"
        username = self.user['email']
        password = self.user['password']

        pp = poplib.POP3_SSL(host)

        pp.user(username)
        pp.pass_(password)
        ret=pp.stat()
        mailnum=ret[0]
        hdr,message,octet = pp.retr(mailnum)


        mail=email.message_from_bytes('n'.encode('utf-8').join(message))
        self.subject = email.header.decode_header(mail.get('subject'))
        if self.subject[0][0] == 'shutdown':
            pp.dele(mailnum)
        pp.quit()
        #print(subject[0][0])
        #print(subject[0][0] == 'test')
    def shutdowndown(self):
        self.accept_mail()
        if self.subject[0][0] == 'shutdown':
            self.send_mail()
            cmd="cmd.exe /k shutdown -s -t 0"
            os.system(cmd)
            pass
    def send_mail(self):
        msg=email.mime.multipart.MIMEMultipart()  
        msg['from']=self.user['email'] 
        msg['to']=self.user['reemail'] 
        msg['subject']='Done!!!'  
        content=''''' 
             主人,
             您的电脑已成功关机!                          
              '''  
        txt=email.mime.text.MIMEText(content)  
        msg.attach(txt)  
        smtp=smtplib  
        smtp=smtplib.SMTP()  
        smtp.connect('smtp.163.com','25')
        smtp.login(self.user['email'],self.user['password'])
        smtp.sendmail(self.user['email'],self.user['reemail'],str(msg))  
        smtp.quit()     
        
#if __name__ == '__main__':
     # import sys
from PyQt4 import QtGui
    
app = QtGui.QApplication(sys.argv)
dlg = Dialog()
dlg.show()
sys.exit(app.exec_())

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读