# -*- 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_())