Python指令碼實現DNSPod DNS動態解析網域名稱

來源:互聯網
上載者:User
閑暇之餘,在家裡自建了個伺服器,因為用的小區寬頻,IP位動態分配。網域名稱解析就是個問題,我的網域名稱一般停放在DNSPod下。DNSPod有提供修改的API,就用Python簡單的實現了一下動態解析。這樣,就不用安裝花生殼了。 廢話不說,看代碼:

#!/usr/bin/env python#-*- coding:utf-8 -*-import httplib, urllib, urllib2import timeimport sys,osimport reimport jsonusername = 'xxxx' #帳號password = 'xxx' #密碼format = 'json'domain = [u'www.youdomain.com'] #要解析的網域名稱def get_domain_info(domain):  domain_split = domain.split('.')  domain_split_len = len(domain_split)  maindomain = domain_split[domain_split_len - 2] + '.' + domain_split[domain_split_len - 1]  return maindomain,domainparams = {'login_email':username,'login_password':password,'format':format}def request(action, params, method = 'POST'):  headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}  conn = httplib.HTTPSConnection("dnsapi.cn")  conn.request(method, '/' + action, urllib.urlencode(params), headers)  response = conn.getresponse()  data = response.read()  conn.close()  if response.status == 200:    return data  else:    return Nonedef get_my_domain_id():  data = request('Domain.List',params)  data = json.loads(data)  domainlist = data.get('domains')  domaninfo = {}  for d in domainlist:    domaninfo[d.get('name')] = d.get('id')  return domaninfodef get_my_domain_record_id(domain_id):  params['domain_id'] = domain_id  data = request('Record.List',params)  data = json.loads(data)  if data.get('code') == '10':    return None  domainname = data.get('domain').get('name')  record_list = data.get('records')  record = {}  for r in record_list:    if r.get('type') == 'A':      key = r.get('name') != '@' and r.get('name') + '.' + domainname or domainname      record[key] = {'id':r.get('id'),'value':r.get('value')}  return recorddef changerecord(domain,domain_id,record_id,ip):  params['domain_id'] = domain_id  params['record_id'] = record_id  params['record_type'] = 'A'  params['record_line'] = '預設'  params['sub_domain'] = domain  params['ttl'] = 600  params['value'] = ip  data = request('Record.Modify',params)def getip():  url = 'http://iframe.ip138.com/ic.asp'  response = urllib2.urlopen(url)  text = response.read()  ip = re.findall(r'\d+.\d+.\d+.\d+', text)  return ip[0] or Nonedef updatedomaininfo(domain):  m,sub_m = get_domain_info(domain)  domain_id = my_domain_id_list.get(m)  record_list = get_my_domain_record_id(domain_id)  if record_list == None:    return None  rocord_info = record_list.get(sub_m)  record_ip = rocord_info.get('value')  record_id = rocord_info.get('id')  return sub_m,record_ip,record_id,domain_idif __name__ == '__main__':  my_domain_id_list = get_my_domain_id()  try:    for dm in domain:      domaindata = updatedomaininfo(dm)      if domaindata == None:        continue      dnsdomain,dnsdmainip,record_id,domain_id = domaindata      domain_name = dnsdomain.split('.')[0]      ip = getip()      if ip == dnsdmainip:        continue      else:        changerecord(domain_name,domain_id,record_id,ip)  except:    pass
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.