Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ddns power by dnspod api
#!/usr/bin/env python
# -*- coding=utf-8 -*-
# Author: 易思龙 <ixxoo.me@gmail.com>
# Create: 2015-09-05
import time
import json
import urllib
import urllib2
def log(*args):
prefix = time.strftime('%Y-%m-%d %H:%M:%S')
msg = [str(arg) for arg in args]
print '%s | %s' % (prefix, ' '.join(msg))
def web_post(url, param, is_json=True):
log("req: %s, %s" % (url, param))
req = urllib2.Request(url, urllib.urlencode(param))
resp = urllib2.urlopen(req)
data = resp.read()
if is_json:
data = json.loads(data)
log("res: %s, %s" % (url, data))
return data
def web_get(url, param=None, is_json=True):
log("req: %s, %s" % (url, param))
if param:
param = urllib.urlencode(param)
resp = urllib2.urlopen(url, param)
data = resp.read()
if is_json:
data = json.loads(data)
log("res: %s, %s" % (url, data))
return data
def get_ip():
ip = web_get('http://ns1.dnspod.net:6666', is_json=False)
return ip.strip()
class Dnspod(object):
auth_url = 'https://api.dnspod.com/Auth'
domain_list_url = 'https://api.dnspod.com/Domain.List'
record_list_url = 'https://api.dnspod.com/Record.List'
add_record_url = 'https://api.dnspod.com/Record.Create'
update_record_url = 'https://api.dnspod.com/Record.Modify'
def __init__(self, email, pwd, domain, sub):
self.login_email = email
self.login_password = pwd
self.user_token = None
self.domain = domain
self.sub_domain = sub
self.record = None
def auth(self):
if self.user_token:
return True
param = {'login_email': self.login_email, 'login_password': self.login_password, 'format': 'json'}
auth = web_post(self.auth_url, param)
if 'user_token' not in auth:
return False
self.user_token = auth['user_token']
return True
def get_domain_id(self):
if not self.user_token:
return False
param = {'user_token': self.user_token, 'format': 'json'}
domain_list = web_post(self.domain_list_url, param)
if 'domains' not in domain_list:
return False
for domain in domain_list['domains']:
if domain['name'] == self.domain:
self.domain = domain
return True
return False
def get_record_id(self):
if not self.domain['id']:
return False
param = {'user_token': self.user_token, 'domain_id': self.domain['id'], 'format': 'json'}
record_list = web_post(self.record_list_url, param)
if 'records' not in record_list:
return False
for record in record_list['records']:
if record['name'] == self.sub_domain:
self.record = record
return True
return False
def add_record(self, ip):
param = {
'user_token': self.user_token,
'domain_id': self.domain['id'],
'sub_domain': self.sub_domain,
'record_type': 'A',
'record_line': 'default',
'value': ip,
'format': 'json'
}
record = web_post(self.add_record_url, param)
if 'record' not in record:
return False
self.record = {'value': ip}
self.record.update(record['record'])
return True
def update_record(self, ip):
if not self.record:
return False
if self.record['value'] == ip:
return True
log('ip not equal: %s != %s' % (self.record['value'], ip))
param = {
'user_token': self.user_token,
'domain_id': self.domain['id'],
'record_id': self.record['id'],
'sub_domain': self.sub_domain,
'record_type': 'A',
'record_line': 'default',
'value': ip,
'format': 'json'
}
record = web_post(self.update_record_url, param)
if 'record' not in record:
return False
self.record.update(record['record'])
return True
if __name__ == '__main__':
import sys
email, passwd, domain, sub = sys.argv[1:5]
dnspod = Dnspod(email, passwd, domain, sub)
if not dnspod.auth():
log('auth failed')
sys.exit(1)
if not dnspod.get_domain_id():
log('get domain failed')
sys.exit(2)
if not dnspod.get_record_id():
log('get record failed, try add record')
now_ip = get_ip()
if not dnspod.add_record(now_ip):
log('add record fialed')
sys.exit(3)
while True:
time.sleep(5)
now_ip = get_ip()
dnspod.update_record(now_ip)
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.