Tutorial on using Cloudflare API to dynamically modify domain name DNS records (Python code)

888u

Last update at :2024-03-23,Edit by888u

Many domain names on this site are resolved by Cloudflare to facilitate unified management. Today I would like to share with you a tutorial on how to use Cloudflare API. Common Python code can be used to dynamically modify domain name DNS records to point to the target IP address.

1. Cloudflare API application

First, make sure that our domain name has been resolved through Cloudflare. For the setup tutorial, please refer to the previous instructions:

  • "Domain name resolution tutorial: Cloudflare resolution and DNSPod resolution"

You will need to apply for a Cloudflare API Tokens later, application address: https://dash.cloudflare.com/profile/api-tokens

Grant permission to modify DNS:

The next step is to find the Zone ID of the domain name. The Cloudflare Zone ID is different for each domain name and can be seen on the domain name homepage:

2. Cloudflare DNS record dynamics

Share the Python code directly, which is divided into two steps: first obtain the ID of the domain name DNS record, and then modify the parsing record through the ID. Here, take the A record as an example.

1. Get the ID of the DNS record

dns_name is the resolved domain name address, such as www.vpsgo.com

def get_record_id(dns_name, zone_id, token): resp = requests.get( 'https://api.cloudflare.com/client/v4/zones/{}/dns_records'.format(zone_id), headers={ 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' }) if not json.loads(resp.text)['success']: return None domains = json.loads(resp.text)['result'] for domain in domains: if dns_name == domain['name']: return domain['id'] return None

2. Update this DNS record

dns_id is the id obtained in the previous step, ip is the target ip that needs to be resolved, and proxied is whether Cloudflare CDN is enabled

def update_dns_record(dns_name, zone_id, token, dns_id, ip, proxied=False): resp = requests.put( 'https://api.cloudflare.com/client/v4/zones/{}/dns_records/{}'.format( zone_id, dns_id), json={ 'type': 'A', 'name': dns_name, 'content': ip, 'proxied': proxied }, headers={ 'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json' }) if not json.loads(resp.text)['success']: return False return True

The above is the tutorial shared by this website (www.vpsgo.com) on how to use Cloudflare API to dynamically modify domain name DNS records in Python. It is very simple and convenient.

If you need a cheap domain name, you can refer to:

For recommendations on cheap domain names and sharing of domain name resolution tutorials, you can refer to the previous tutorials on this site:

  • NameSilo 1 USD discount code: "Compilation of the latest NameSilo discount codes and promotions, $1 discount available》
  • $5 discount for new Dynadot users: "Share the $5 Dynadot discount code: .com domain name $6.99 for the first year/free privacy protection"
  • Analysis settings Tutorial: "Domain name resolution tutorial: Cloudflare resolution and DNSPod resolution"

Recommended site search: Mainland China permanent free cloud server, vps host, foreign website space, Hong Kong cloud host, aaa server, app Server rental, how to purchase virtual host, special price space, register international domain name overseas virtual host space,

Tutorial on using Cloudflare API to dynamically modify domain name DNS records (Python code)

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码