Skip to content

Instantly share code, notes, and snippets.

@Suzhou65
Last active October 11, 2023 19:27
Show Gist options
  • Select an option

  • Save Suzhou65/3488991186cbf6749b20dfc2ff5dea79 to your computer and use it in GitHub Desktop.

Select an option

Save Suzhou65/3488991186cbf6749b20dfc2ff5dea79 to your computer and use it in GitHub Desktop.
Get IPv4, IPv6 DNS record from CloudFlare API

Let's try to ask DNS record for CloudFlare API. and separate into string data.

The "id" of domain (inclouding subdomain) can be found in JSON data. in this example, it will be filled in "zone_record_ipv4_id" and "zone_record_ipv6_id".

import json
import requests

#API
auth_key = "API Token"
zone_id = "278035ad7a9d983bc54a990b43ef7eb0"
zone_record_ipv4_id = "3f7a9d18e117a65860dc5e2f2abdd191"
zone_record_ipv6_id = "ec8b33016fccf46dc8969316578974d7"

cloudflare_api = "https://api.cloudflare.com/client/v4/"
headers = {'Authorization': auth_key, 'Content-Type':'application/json'}

#IPv4 DNS Record (A)
cloudflare_dns_ipv4 = cloudflare_api + "zones/" + zone_id + "/dns_records/" + zone_record_ipv4_id 
cloudflare_dns_ipv4_respon = requests.get(cloudflare_dns_ipv4, headers=headers)

if cloudflare_dns_ipv4_respon.status_code == 200:
    print("DNS Record A")
else:
    print(cloudflare_dns_ipv4_respon.status_code)

dns_ipv4_json = json.loads(cloudflare_dns_ipv4_respon.text)
record_a = dns_ipv4_json.get('result', {}).get('content')

#IPv6 DNS Record (AAAA)
cloudflare_dns_ipv6 = cloudflare_api + "zones/" + zone_id + "/dns_records/" + zone_record_ipv6_id 
cloudflare_dns_ipv6_respon = requests.get(cloudflare_dns_ipv6, headers=headers)

if cloudflare_dns_ipv6_respon.status_code == 200:
    print("DNS Record AAAA")
else:
    print(cloudflare_dns_ipv6_respon.status_code)

dns_ipv6_json = json.loads(cloudflare_dns_ipv6_respon.text)
record_aaaa = dns_ipv6_json.get('result', {}).get('content')

If the request send successfully, the result will print.

DNS Record A
DNS Record AAAA

Add following codes for asking the result, and it's data type

data_type_4 = type(record_a)
print(data_type_4)
print(record_a)

data_type_6 = type(record_aaaa)
print(data_type_6)
print(record_aaaa)

It will print

<class 'str'>
114.514.19.19

<class 'str'>
8930:8100:1145:141:919:36:114:514
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment