Created
January 13, 2012 00:04
-
-
Save allanlei/1603925 to your computer and use it in GitHub Desktop.
Script to use DNSimple's API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import simplejson as json | |
import urllib2, urllib | |
import httplib | |
import base64 | |
parser = argparse.ArgumentParser(description='Updates DNSimple with EC2\'s hostname') | |
parser.add_argument('cname', type=str, nargs=1, help='Name of the CNAME') | |
parser.add_argument('--domain', default='rhinoaccounting.com', help='Domain to update') | |
parser.add_argument('--value', default='thor.rhinoaccounting.com', help='CNAME reference') | |
parser.add_argument('--user', required=True, help='DNSimple username') | |
parser.add_argument('--token', required=True, help='DNSimple user token') | |
args = parser.parse_args() | |
request = urllib2.Request('https://dnsimple.com/domains/{domain}/records'.format(domain=args.domain), data=urllib.urlencode({ | |
'record': { | |
'name': args.cname[0], | |
'record_type': 'CNAME', | |
'content': args.value, | |
'ttl': 3600, | |
} | |
})) | |
request.headers['Accept'] = 'application/json' | |
#request.headers['Content-Type'] = 'application/json' | |
request.headers['X-DNSimple-Token'] = '{user}:{token}'.format(user=args.user, token=args.token) | |
request.get_method = lambda: 'POST' | |
print request.get_method(), request.get_full_url() | |
print request.headers | |
print request.get_data() | |
try: | |
response = urllib2.urlopen(request) | |
print response.read() | |
except urllib2.HTTPError, err: | |
print err.code, err.read() | |
print err.info() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment