#!/usr/bin/env python #-*- coding:utf-8 -*- import urllib2,urllib,json,socket # Based on roy@leadnt.com code class Dns: #Dnspod账户 _dnspod_user = 'dnspod_username' #Dnspod密码 _dnspod_pwd = 'dnspod_password' #Dnspod主域名,注意:是你注册的域名 _domain = 'your domain' #子域名,如www,如果要使用根域名,用@ _sub_domain = 'your subdomain' def getIP(self): '''get local IP based on socket. Try to connect baidu.com, then getsockname''' s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('baidu.com', 0)) return s.getsockname()[0] def api_call(self,api,data): try: api = 'https://dnsapi.cn/' + api data['login_email'] = self._dnspod_user data['login_password'] = self._dnspod_pwd data['format'] ='json' data['lang'] = 'cn' data['error_on_empty'] = 'no' data = urllib.urlencode(data) req = urllib2.Request(api,data, headers = { 'UserAgent' : 'LocalDomains/1.0.0', 'Content-Type':'application/x-www-form-urlencoded;text/html; charset=utf8', }) res = urllib2.urlopen(req) html = res.read() results = json.loads(html) return results except Exception as e: print e def main(self): ip = self.getIP() dinfo = self.api_call('domain.info',{'domain' : self._domain}) domainId = dinfo['domain']['id'] rs = self.api_call('record.list', { 'domain_id': domainId, 'offset' :'0', 'length' : '1', 'sub_domain' : self._sub_domain }) if rs['info']['record_total'] == 0: self.api_call('record.create', { 'domain_id' : domainId, 'sub_domain' : self._sub_domain, 'record_type' : 'A', 'record_line' : '默认', 'value' : ip, 'ttl' : '3600' }) print 'Success.' else: if rs['records'][0]['value'].strip() != ip.strip(): self.api_call('record.modify', { 'domain_id' : domainId, 'record_id' : rs['records'][0]['id'], 'sub_domain' : self._sub_domain, 'record_type' : 'A', 'record_line' : '默认', 'value' : ip }) else: print 'Success.' if __name__ == '__main__': d = Dns(); d.main()