python通过微信发送邮件实现电脑关机
作者:shhchen 时间:2022-03-20 17:58:37
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()
来源:https://blog.csdn.net/shhchen/article/details/52451040
标签:python,微信,电脑关机
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server修改表所有者
2010-02-04 08:33:00
python链表的基础概念和基础用法详解
2021-02-26 07:13:50
![](https://img.aspxhome.com/file/2023/5/80435_0s.jpg)
python Flask 装饰器顺序问题解决
2022-09-30 09:16:42
Python中WebService客户端接口调用及身份验证的问题
2021-12-22 06:01:05
python写xml文件的操作实例
2023-08-09 00:40:39
python按行读取文件,去掉每行的换行符\\n的实例
2022-06-01 03:49:43
Python webargs 模块的简单使用
2021-02-27 11:56:04
Python绘制正余弦函数图像的方法
2022-09-22 19:59:06
![](https://img.aspxhome.com/file/2023/8/104888_0s.png)
Python爬虫爬取Bilibili弹幕过程解析
2021-11-26 02:58:49
![](https://img.aspxhome.com/file/2023/5/95755_0s.png)
PHP实现生成二维码的示例代码
2023-06-13 09:16:03
Python中的模块和包概念介绍
2023-06-06 09:13:53
Python使用shutil操作文件、subprocess运行子程序
2021-03-01 00:19:19
anaconda升级sklearn版本的实现方法
2021-08-07 02:28:18
![](https://img.aspxhome.com/file/2023/9/93879_0s.png)
你知道吗实现炫酷可视化只要1行python代码
2022-06-10 13:36:16
![](https://img.aspxhome.com/file/2023/6/87686_0s.jpg)
在Oracle网络结构解决连接问题
2010-07-28 12:49:00
Python获取Linux系统下的本机IP地址代码分享
2021-07-23 00:26:22
asp如何让页面过时并指定一个过时时间?
2010-05-13 16:40:00
python 线性回归分析模型检验标准--拟合优度详解
2022-07-24 08:29:49
![](https://img.aspxhome.com/file/2023/8/120358_0s.jpg)
ASP Framework_1_简介
2009-10-12 11:35:00
Python+appium框架原生代码实现App自动化测试详解
2023-02-26 11:59:32
![](https://img.aspxhome.com/file/2023/9/127929_0s.jpg)