python實現微信發(fā)送郵件關閉電腦功能
更新時間:2018年02月22日 15:14:45 作者:shhchen
這篇文章主要介紹了python實現微信發(fā)送郵件關閉電腦功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
Python 通過微信郵件實現電腦關機,供大家參考,具體內容如下
通過手機微信發(fā)送QQ郵件給sina郵箱,然后利用python的pop3定時檢查sina郵箱的郵件主題以及郵件來源,并在電腦執(zhí)行相應的命令行實現關機。
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)
#根據判斷結果,執(zhí)行操作
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()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
關于初始種子自動選取的區(qū)域生長實例(python+opencv)
今天小編就為大家分享一篇關于初始種子自動選取的區(qū)域生長實例(python+opencv),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
python計算機視覺OpenCV庫實現實時攝像頭人臉檢測示例
這篇文章主要為大家介紹了python使用OpenCV實現實時攝像頭人臉檢測的示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10

