Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Martin J. Levy edited this page May 6, 2016 · 10 revisions

#Examples

There is an example folder with many more coding examples included.

A simple example

This short example prints the name of all your zones and if IPv6 is enabled for that specific zone. This code can be found at example-are-zones-ipv6-simple.py

#!/usr/bin/env python
import CloudFlare
def main():
	cf = CloudFlare.CloudFlare()
	zones = cf.zones.get(params={'per_page':50})
	for zone in zones:
		zone_name = zone['name']
		zone_id = zone['id']
		settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
		ipv6_on = settings_ipv6['value']
		print zone_id, ipv6_on, zone_name
	exit(0)
if __name__ == '__main__':
	main()

A DNS example

Here's a DNS example showing how to set values. This code can be found at example-create-zone-and-populate.py

#!/usr/bin/env python

import os
import sys
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

def main():
	try:
		zone_name = sys.argv[1]
	except:
		exit('usage: provide a zone name as an argument on the command line')

	cf = CloudFlare.CloudFlare()

	# Create zone - which will only work if ... 1) The zone is not on CloudFlare. 2) The zone passes a whois test
	print 'Create zone %s ...' % (zone_name)
	try:
		zone_info = cf.zones.post(data={'jump_start':False, 'name': zone_name})
	except CloudFlare.CloudFlareAPIError as e:
		exit('/zones.post %s - %d %s' % (zone_name, e, e))
	except Exception as e:
		exit('/zones.post %s - %s' % (zone_name, e))

	zone_id = zone_info['id']
	if 'email' in zone_info['owner']:
		zone_owner = zone_info['owner']['email']
	else:
		zone_owner = '"' + zone_info['owner']['name'] + '"'
	zone_plan = zone_info['plan']['name']
	zone_status = zone_info['status']
	print '\t%s name=%s owner=%s plan=%s status=%s\n' % (zone_id, zone_name, zone_owner, zone_plan, zone_status)

	# DNS records to create
	dns_records = [
		{'name':'ding', 'type':'A', 'content':'216.58.194.206'},
		{'name':'foo', 'type':'AAAA', 'content':'2001:d8b::1'},
		{'name':'foo', 'type':'A', 'content':'192.168.0.1'},
		{'name':'duh', 'type':'A', 'content':'10.0.0.1', 'ttl':120},
		{'name':'bar', 'type':'CNAME', 'content':'foo.mahtin.net'},
		{'name':'shakespeare', 'type':'TXT', 'content':"What's in a name? That which we call a rose by any other name would smell as sweet."}
	]

	print 'Create DNS records ...'
	for dns_record in dns_records:
		# Create DNS record
		try:
			r = cf.zones.dns_records.post(zone_id, data=dns_record)
		except CloudFlare.CloudFlareAPIError as e:
			exit('/zones.dns_records.post %s %s - %d %s' % (zone_name, record['name'], e, e))
		# Print respose info - they should be the same
		dns_record = r
		print '\t%s %30s %6d %-5s %s ; proxied=%s proxiable=%s' % (dns_record['id'], dns_record['name'], dns_record['ttl'], dns_record['type'], dns_record['content'], dns_record['proxied'], dns_record['proxiable'])

		# set proxied flag to false - for example
		dns_record_id = dns_record['id']

		new_dns_record = {
			# Must have type/name/content (even if they don't change)
			'type':dns_record['type'],
			'name':dns_record['name'],
			'content':dns_record['content'],
			# now add new values you want to change
			'proxied':False
		}

		try:
			dns_record = cf.zones.dns_records.put(zone_id, dns_record_id, data=new_dns_record)
		except CloudFlare.CloudFlareAPIError as e:
			exit('/zones/dns_records.put %d %s - api call failed' % (e, e))

	print ''

	# Now read back all the DNS records
	print 'Read back DNS records ...'
	try:
		dns_records = cf.zones.dns_records.get(zone_id)
	except CloudFlare.CloudFlareAPIError as e:
		exit('/zones.dns_records.get %s - %d %s' % (zone_name, e, e))

	for dns_record in sorted(dns_records, key=lambda v: v['name']):
		print '\t%s %30s %6d %-5s %s ; proxied=%s proxiable=%s' % (dns_record['id'], dns_record['name'], dns_record['ttl'], dns_record['type'], dns_record['content'], dns_record['proxied'], dns_record['proxiable'])

	print ''

	exit(0)

if __name__ == '__main__':
	main()

A setting values for a zone example

Here's an example showing how to adjust values for a zone. In this case, loop thru all your zones and enable IPv6. This code can be found at example-are-zones-ipv6.py

#!/usr/bin/env python

import os
import sys
sys.path.insert(0, os.path.abspath('..'))
import CloudFlare

def main():

	# Check for update flag
	update_ipv6 = False
	try:
		if sys.argv[1] == '--update':
			update_ipv6 = True
			sys.argv.pop(1)
	except:
		pass

	# Grab the first argument, if there is one
	try:
		zone_name = sys.argv[1]
		params = {'name':zone_name,'per_page':1}
	except:
		params = {'per_page':50}

	cf = CloudFlare.CloudFlare()

	# grab the zone identifier
	try:
		zones = cf.zones.get(params=params)
	except CloudFlare.CloudFlareAPIError as e:
		exit('/zones.get %d %s - api call failed' % (e, e))
	except Exception as e:
		exit('/zones - %s - api call failed' % (e))

	for zone in sorted(zones, key=lambda v: v['name']):
		zone_name = zone['name']
		zone_id = zone['id']
		try:
			ipv6 = cf.zones.settings.ipv6.get(zone_id)
		except CloudFlare.CloudFlareAPIError as e:
			exit('/zones.settings.ipv6.get %d %s - api call failed' % (e, e))

		ipv6_value = ipv6['value']
		if update_ipv6 and ipv6_value == 'off':
			print zone_id, ipv6_value, zone_name, '(now updating... off -> on)'
			try:
				ipv6 = cf.zones.settings.ipv6.patch(zone_id, data={'value':'on'})
			except CloudFlare.CloudFlareAPIError as e:
				exit('/zones.settings.ipv6.patch %d %s - api call failed' % (e, e))
			ipv6_value = ipv6['value']
			if ipv6_value == 'on':
				print '\t', '... updated!'
		else:
			print zone_id, ipv6_value, zone_name

	exit(0)

if __name__ == '__main__':
	main()

Clone this wiki locally

Morty Proxy This is a proxified and sanitized view of the page, visit original site.