Loading... ## 检测公网ip是否改变 ```python import requests,json,time import smtplib from email.mime.text import MIMEText import schedule from time import sleep import datetime def get_163(IP): # coding:utf -8 to_list =["123456@qq.com","456789@126.com"]#两个接收消息的邮箱 # 邮件构建 subject = "滴滴答答" # 邮件标题 sender = "123456@126.com" # 发送方 content = "您的群辉服务器地址改变请注意!{}".format(IP) #recver = "123456@qq.com" # 接收方 password = "passwd"#邮箱密码 message = MIMEText(content, "plain", "utf-8") # content 发送内容 "plain"文本格式 utf-8 编码格式 message['Subject'] = subject # 邮件标题 message['To'] = ";".join(to_list) # 收件人 message['From'] = sender # 发件人 try: smtp = smtplib.SMTP_SSL("smtp.126.com", 994) # 实例化smtp服务器 smtp.login(sender, password) # 发件人登录 smtp.sendmail(sender, to_list, message.as_string()) # as_string 对 message 的消息进行了封装 smtp.quit() smtp.close() except Exception as e: print('error', e) # 打印错误 def get_ip(): global ip try: res = requests.get('http://httpbin.org/ip') new_ip = json.loads(res.text)['origin'] if ip == '': ip = json.loads(res.text)['origin'] print('当前IP', ip) time.sleep(1) get_163(new_ip) if new_ip != ip: print('IP切换发送邮箱') get_163(new_ip) ip = new_ip else: #print(ip,new_ip) print('IP没有变化!') except Exception as e: pass if __name__ == '__main__': ip = "" # 定时 # 每两个小时执行一次任务 schedule.every(5).seconds.do(get_ip) while True: print("正在运行中,当前时间" + str(datetime.datetime.now()),ip) schedule.run_pending() sleep(2) ``` 最后修改:2022 年 12 月 02 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏