Skip to content

Instantly share code, notes, and snippets.

@zeerorg
Created January 5, 2019 14:22
Show Gist options
  • Save zeerorg/f375193c3f0628f03a9cf4c0380e973e to your computer and use it in GitHub Desktop.
Save zeerorg/f375193c3f0628f03a9cf4c0380e973e to your computer and use it in GitHub Desktop.
Effectively use cloudflare as dynamic dns provider
#!/usr/bin/python3
import json
import urllib.request
authkey = ''
email = ''
zone_id = ''
dns_id = ''
my_ip = urllib.request.urlopen('https://api.ipify.org').read().decode('utf-8')
cld_upd = 'https://api.cloudflare.com/client/v4/zones/' + zone_id + '/dns_records/' + dns_id
my_headers = {'X-Auth-Email': email, 'X-Auth-Key': authkey, 'Content-Type': 'application/json'}
data = json.dumps({
"type": "A",
"name": "home.zeerorg.site",
"content": my_ip,
"proxied": False
}).encode('utf-8')
dns_update = urllib.request.Request(cld_upd, data=data, headers=my_headers, method='PUT')
print(json.dumps(json.loads(urllib.request.urlopen(dns_update).read().decode('utf-8')), indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment