Chen Di's Blog

示例代码丨使用API更新GoDaddy DNS记录


在某些情况下,我们的服务器可能会频繁更换 IP 地址,例如使用家庭宽带的动态 IP 或云服务器的弹性 IP。每次更改 DNS 解析时,都需要登录 GoDaddy 进行修改,有时还会触发邮箱验证,十分麻烦。因此,我们可以使用 GoDaddy API 结合 Python 脚本自动更新 DNS 解析。

获取 GoDaddy API 密钥

Python 脚本实现

import requests  
  
api_key = 'YOUR_API_KEY'  
api_secret = 'YOUR_API_SECRET'  
base_url = 'https://api.godaddy.com/v1'  
  
headers = {  
    'Authorization': f'sso-key {api_key}:{api_secret}',  
    'Content-Type': 'application/json'  
}  

def update_dns_record(domain_name, record_type, record_name, value):  
    url = f'{base_url}/domains/{domain_name}/records/{record_type}/{record_name}'  
    data = [  
        {  
            'data': value,  
            'ttl': 3600  
        }  
    ]  
    response = requests.put(url, headers=headers, json=data)  
    if response.status_code == 200:  
        print(f'DNS record updated successfully.')  
    else:  
        print(f'Error: {response.status_code}')  

# 示例:更新example.com的A记录  
update_dns_record('example.com', 'A', '@', '192.168.1.1')