|
安装pydnspod第三方库
1. 安装requests
pip3 install requests
2. 安装dns-dnspod
pip3 install dns-dnspod
3. 创建log.txt 空文件
4. 创建xxxx.py输入下面代码
5.创建定时任务
完整代码
import time
import re
import requests
import pydnspod
def get_local_ipv4():
html_text = requests.get("https://ip.cn/").text
ip_text = re.findall(r"\b(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", html_text)
return ip_text[0]
user_id = ""#更换成你的Token ID
user_token = ""#更换成你的Token
domain = "baidu.com"#更换成你的域名
sub_domain = "hass"#更换成你的记录
sub_domain_id = ""#更换成你的记录ID
lgo_txt='./log.txt'
file_object = open(lgo_txt)#打开文件
ipv4 = get_local_ipv4()
try:
file_context = file_object.read().rstrip()
if ipv4!=file_context:
wfile=open(lgo_txt,'w')
wfile.write(ipv4)
wfile.close()
dp = pydnspod.connect(user_id,user_token)
return_ = dp.record.modify(domain,sub_domain_id,sub_domain,"A",ipv4)
print('已更新')
else:
print('不需要更新')
finally:
file_object.close()
|
|