Python script implementation dnspod DNS dynamic resolution domain name

Source: Internet
Author: User
Spare time, in the home built a server, because the use of the cell broadband, IP bit dynamic allocation. Domain name resolution is a problem, my domain name is generally parked under the dnspod. Dnspod has the API to provide the modification, the use of Python simple implementation of the dynamic analysis. This way, you don't have to install a peanut shell. Nonsense don't say, look at the code:

#!/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 (DOM  Ain): Domain_split = Domain.split ('. ') Domain_split_len = Len (domain_split) Maindomain = domain_split[domain_split_len-2] + '. ' + Domain_split[domain_split_le N-1] Return maindomain,domainparams = {' Login_email ': username, ' Login_password ':p assword, ' 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.getr Esponse () 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_domai N_RECORD_ID (domain_id): params[' domain_id '] = domain_id data = Request (' record.list ', params) data = json.loads (data) I   F data.get (' code ') = = ' Ten ': 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 (DOMA   IN,DOMAIN_ID,RECORD_ID,IP): params[' domain_id '] = domain_id params[' record_id '] = record_id params[' record_type '] = ' A ' params[' record_line ' = ' default ' params[' sub_domain '] = domain params[' ttl '] = + params[' value '] = IP data = Request (' Record.modify ', params) def getip (): url = ' http://iframe.ip138.com/ic.asp ' response = urllib2.urlopen (URL) Text = Respon Se.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 Domai N: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,recor D_ID,IP) Except:pass
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.