树莓派上报IP到QQ邮箱

https://zhuanlan.zhihu.com/p/21471896

上古PYTHON2代码魔改居然跑起来呜呜呜呜

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import smtplib
import string
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import urllib.request

def do_it():#好吧
    fromaddr = "hello@wandeshima.com"  # 填写你的发信邮箱,我选用的是163邮箱
    toaddr = "m@wandeshima.com"   # 填写你的收信地址,接收树莓派的公网IP地址
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = '树莓派IP是 ' + ip   # 邮件标题

    body = '树莓派IP是 ' + ip   # 邮件内容,同标题(偷懒)
    msg.attach(MIMEText(body, 'plain'))

    server = smtplib.SMTP_SSL('smtp.qq.com', 465)   # 填写qq邮箱的发信服务器地址
    server.login(fromaddr, "xxxxxxxxxx")   # xxx代表你的邮件登录密码
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text) # 开始发邮件
    print("send ok")  # 发送成功提示
    server.quit()


#file_path config
#file_path = "/root/rootcrons/lastip.txt"
file_path = "/home/mmmm/Desktop/lastip.txt"

user_agent = 'Mozilla/4.0 (compatible; MSIE 10.0; Windows NT)'
headers = {}
headers['User-Agent'] = user_agent
url = 'http://members.3322.org/dyndns/getip'   # 通过这个接口获取公网IP地址
req = urllib.request.Request(url, headers = headers)
res = urllib.request.urlopen(req)

ip = res.read().strip().decode("utf-8")  # 去除空格
print(ip)

ip_file = open(file_path)
last_ip = ip_file.read()
ip_file.close()

if last_ip == ip:
    print("IP not change.")
else:
    print("IP changed. New ip: "+ ip)
    ip_file = open(file_path,"w")
    ip_file.write(str(ip))
    ip_file.close()
    do_it()
https://blog.csdn.net/qq_16234613/article/details/79448203

https://blog.csdn.net/mr_muli/article/details/78573156

http://www.runoob.com/python3/python3-string-strip.html

https://github.com/laixintao/Report-IP-hourly

整完发现路由没有公网IP,白瞎了。。。