参照 原文
增加了在指定时间判断网络是否连接,只要是在指定时间联网就可以发送邮件,超过指定时间不发送邮件。
具体代码如下:
发送邮箱配置:参见smtplib +email 发送邮件,qq
1 脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText
import datetime
import requests
import time
def sendmail(subject,content):
email_host = 'smtp.qq.com' #可更改为其他邮箱
email_user = '更改为发件人qq邮箱或其他邮箱,@qq.com'
email_pwd = '发送qq邮箱的授权码'
maillist = '收件人邮箱'
me = email_user
#content = "this is a test"
msg = MIMEText(content,'plain','utf-8')
msg['Subject'] = subject
msg['From'] = me
msg['To'] = maillist
smtp = smtplib.SMTP(email_host)
smtp.login(email_user,email_pwd)
smtp.sendmail(me,maillist,msg.as_string())
smtp.quit()
#print("email send success")
def isConnected():
"""规定时间确定网络是否连接,6mins"""
while True:
for i in range(120):
try:
html = requests.get('https://www.baidu.com',timeout=2)
except:
#return i
time.sleep(3)
else:
# print("联网成功")
# print(str(i))
return True
break
if i == 1:
return False
if __name__ == '__main__':
if isConnected():
try:
aa = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
content = '您的计算机已开机,开机时间约为: {}'.format(aa)
subject = "计算机已开机"
sendmail(subject=subject,content=content)
except Exception as e:
print(str(e))
else:
pass
2 将脚本打包成exe文件 并将文件快捷方式放入开机目录文件夹
设置开机启动参照 设置程序开机启动
pyinstaller kdji.py --noconsole #不显示窗口
网友评论