通过 python 把家里路由的 IP 发邮件给自己

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

# coding=utf-8
import smtplib
import httplib
import time
import imaplib
from email.mime.text import MIMEText
from email.header import Header
import datetime

#从 Oray 网站取 IP 地址
httpClient = httplib.HTTPConnection('ddns.oray.com', 80, timeout=30)
httpClient.request('GET', '/checkip')
response = httpClient.getresponse()
ipline = response.read().split(' ')[5]
ipAddress = ipline[0:ipline.index('<')]
httpClient.close()

# #准备 Email 内容
# #标题是时间
# #内容是 IP 地址
msg = MIMEText("""</pre>
                    <h1>"""+ipAddress+"""</h1>
                    <pre>""",'html','utf-8')
msg['Subject'] = Header(time.strftime('%Y-%m-%d %H:%M:%S'), 'utf-8')
# #发送邮件
so = smtplib.SMTP()
so.connect('smtp.163.com')
print('smtp server connect')
so.login('username', 'password')
so.sendmail('username@163.com','username@sina.com',msg.as_string());
so.close()

#删除前一天记录 IP 的邮件
imap = imaplib.IMAP4('imap.sina.com')
imap.login('username','password')
imap.select("INBOX",False)
beforDay = datetime.datetime.now()+datetime.timedelta(days=-1)

type,data = imap.search(None,'(SUBJECT "'+beforDay.strftime('%Y-%m-%d')+'")')
print(data)
splitData = data[0].split(' ')[::-1]
for num in splitData:
    if num!='':
        imap.store(num, '+FLAGS', '\\Deleted')
imap.expunge()
imap.close()