异想之旅:本人原创博客完全手敲,绝对非搬运,全网不可能有重复;本人无团队,仅为技术爱好者进行分享,所有内容不牵扯广告。本人所有文章仅在CSDN、掘金和个人博客(一定是异想之旅域名)发布,除此之外全部是盗文!
先说需求:我在家里有一台服务器,但是没有静态IP。之前使用的是花生壳免费的服务,但是后来收费了,就想到自己实现一个。
参考资料:
大体的实现思路:
当然,还要加上缓存等东西
主机 = <span class="hljs-string">"yxzlownserveraddress"</span> 域名 = <span class="hljs-string">"yxzl.top"</span> AccessKey = <span class="hljs-string">"AccessKey"</span> SecretKey = <span class="hljs-string">"SecretKey"</span> <span class="hljs-keyword">import</span> json <span class="hljs-keyword">import</span> requests <span class="hljs-keyword">import</span> time <span class="hljs-keyword">from</span> aliyunsdkcore.client <span class="hljs-keyword">import</span> AcsClient <span class="hljs-keyword">from</span> aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest <span class="hljs-keyword">import</span> DescribeDomainRecordsRequest <span class="hljs-keyword">from</span> aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest <span class="hljs-keyword">import</span> UpdateDomainRecordRequest client = AcsClient(AccessKey, SecretKey) 记录ID = 旧的IP = <span class="hljs-string">""</span> page_number = <span class="hljs-number">1</span> <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>: request = DescribeDomainRecordsRequest() request.set_accept_format(<span class="hljs-string">"json"</span>) request.set_DomainName(域名) request.set_PageNumber(page_number) request.set_PageSize(<span class="hljs-number">100</span>) response = client.do_action_with_exception(request) text = <span class="hljs-built_in">str</span>(response, encoding=<span class="hljs-string">"utf-8"</span>) <span class="hljs-built_in">print</span>(json.loads(text)) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> json.loads(text)[<span class="hljs-string">"DomainRecords"</span>][<span class="hljs-string">"Record"</span>]: <span class="hljs-keyword">if</span> i[<span class="hljs-string">"RR"</span>] == 主机: 记录ID = i[<span class="hljs-string">"RecordId"</span>] 旧的IP = i[<span class="hljs-string">"Value"</span>] <span class="hljs-keyword">break</span> <span class="hljs-keyword">if</span> 记录ID: <span class="hljs-keyword">break</span> page_number += <span class="hljs-number">1</span> <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>: <span class="hljs-keyword">try</span>: 新的IP = requests.get(<span class="hljs-string">"http://ip.3322.net/"</span>).text <span class="hljs-keyword">if</span> 新的IP != 旧的IP: <span class="hljs-keyword">try</span>: request = UpdateDomainRecordRequest() request.set_accept_format(<span class="hljs-string">"json"</span>) request.set_RecordId(记录ID) request.set_RR(主机) request.set_Type(<span class="hljs-string">"A"</span>) request.set_Value(新的IP) response = client.do_action_with_exception(request) <span class="hljs-built_in">print</span>(<span class="hljs-string">"%s\tIP发生了变化(%s -> %s),已进行更新。"</span> % (time.strftime( <span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime()), 旧的IP, 新的IP)) <span class="hljs-keyword">except</span>: <span class="hljs-built_in">print</span>(<span class="hljs-string">"%s\tIP发生了变化(%s -> %s),但更新失败。"</span> % (time.strftime( <span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime()), 旧的IP, 新的IP)) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(<span class="hljs-string">"%s\tIP相同(%s),未进行更改。"</span> % (time.strftime(<span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime()), 新的IP)) 旧的IP = 新的IP <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e: <span class="hljs-built_in">print</span>(<span class="hljs-string">"%s\t发生错误:%s"</span> % (time.strftime(<span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime()), e)) <span class="hljs-keyword">pass</span> time.sleep(<span class="hljs-number">90</span>) 主机 = <span class="hljs-string">"yxzlownserveraddress"</span> 域名 = <span class="hljs-string">"yxzl.top"</span> AccessKey = <span class="hljs-string">"AccessKey"</span> SecretKey = <span class="hljs-string">"SecretKey"</span> <span class="hljs-keyword">import</span> json <span class="hljs-keyword">import</span> requests <span class="hljs-keyword">import</span> time <span class="hljs-keyword">from</span> aliyunsdkcore.client <span class="hljs-keyword">import</span> AcsClient <span class="hljs-keyword">from</span> aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest <span class="hljs-keyword">import</span> DescribeDomainRecordsRequest <span class="hljs-keyword">from</span> aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest <span class="hljs-keyword">import</span> UpdateDomainRecordRequest client = AcsClient(AccessKey, SecretKey) 记录ID = 旧的IP = <span class="hljs-string">""</span> page_number = <span class="hljs-number">1</span> <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>: request = DescribeDomainRecordsRequest() request.set_accept_format(<span class="hljs-string">"json"</span>) request.set_DomainName(域名) request.set_PageNumber(page_number) request.set_PageSize(<span class="hljs-number">100</span>) response = client.do_action_with_exception(request) text = <span class="hljs-built_in">str</span>(response, encoding=<span class="hljs-string">"utf-8"</span>) <span class="hljs-built_in">print</span>(json.loads(text)) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> json.loads(text)[<span class="hljs-string">"DomainRecords"</span>][<span class="hljs-string">"Record"</span>]: <span class="hljs-keyword">if</span> i[<span class="hljs-string">"RR"</span>] == 主机: 记录ID = i[<span class="hljs-string">"RecordId"</span>] 旧的IP = i[<span class="hljs-string">"Value"</span>] <span class="hljs-keyword">break</span> <span class="hljs-keyword">if</span> 记录ID: <span class="hljs-keyword">break</span> page_number += <span class="hljs-number">1</span> <span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>: <span class="hljs-keyword">try</span>: 新的IP = requests.get(<span class="hljs-string">"http://ip.3322.net/"</span>).text <span class="hljs-keyword">if</span> 新的IP != 旧的IP: <span class="hljs-keyword">try</span>: request = UpdateDomainRecordRequest() request.set_accept_format(<span class="hljs-string">"json"</span>) request.set_RecordId(记录ID) request.set_RR(主机) request.set_Type(<span class="hljs-string">"A"</span>) request.set_Value(新的IP) response = client.do_action_with_exception(request) <span class="hljs-built_in">print</span>(<span class="hljs-string">"%s\tIP发生了变化(%s -> %s),已进行更新。"</span> % (time.strftime( <span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime()), 旧的IP, 新的IP)) <span class="hljs-keyword">except</span>: <span class="hljs-built_in">print</span>(<span class="hljs-string">"%s\tIP发生了变化(%s -> %s),但更新失败。"</span> % (time.strftime( <span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime()), 旧的IP, 新的IP)) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(<span class="hljs-string">"%s\tIP相同(%s),未进行更改。"</span> % (time.strftime(<span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime()), 新的IP)) 旧的IP = 新的IP <span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e: <span class="hljs-built_in">print</span>(<span class="hljs-string">"%s\t发生错误:%s"</span> % (time.strftime(<span class="hljs-string">"%Y-%m-%d %H:%M:%S"</span>, time.localtime()), e)) <span class="hljs-keyword">pass</span> time.sleep(<span class="hljs-number">90</span>)
主机 = "yxzlownserveraddress" 域名 = "yxzl.top" AccessKey = "AccessKey" SecretKey = "SecretKey" import json import requests import time from aliyunsdkcore.client import AcsClient from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest client = AcsClient(AccessKey, SecretKey) 记录ID = 旧的IP = "" page_number = 1 while True: request = DescribeDomainRecordsRequest() request.set_accept_format("json") request.set_DomainName(域名) request.set_PageNumber(page_number) request.set_PageSize(100) response = client.do_action_with_exception(request) text = str(response, encoding="utf-8") print(json.loads(text)) for i in json.loads(text)["DomainRecords"]["Record"]: if i["RR"] == 主机: 记录ID = i["RecordId"] 旧的IP = i["Value"] break if 记录ID: break page_number += 1 while True: try: 新的IP = requests.get("http://ip.3322.net/").text if 新的IP != 旧的IP: try: request = UpdateDomainRecordRequest() request.set_accept_format("json") request.set_RecordId(记录ID) request.set_RR(主机) request.set_Type("A") request.set_Value(新的IP) response = client.do_action_with_exception(request) print("%s\tIP发生了变化(%s -> %s),已进行更新。" % (time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), 旧的IP, 新的IP)) except: print("%s\tIP发生了变化(%s -> %s),但更新失败。" % (time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), 旧的IP, 新的IP)) else: print("%s\tIP相同(%s),未进行更改。" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), 新的IP)) 旧的IP = 新的IP except Exception as e: print("%s\t发生错误:%s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), e)) pass time.sleep(90)
原文链接:https://juejin.cn/post/7082606430766759966
© 版权声明
声明📢本站内容均来自互联网,归原创作者所有,如有侵权必删除。
本站文章皆由CC-4.0协议发布,如无来源则为原创,转载请注明出处。
THE END