Python script implements dnspod DNS dynamic resolution domain name _python

Source: Internet
Author: User
Tags in domain python script

Spare time, in the home since built a server, because the use of the community broadband, IP bit dynamic allocation. Domain name resolution is a problem, my domain is generally parked under the dnspod. Dnspod has the API to provide the modification, it simply implemented the dynamic parsing with Python. This way, there is no need to install the peanut shell. Nonsense don't say, look code:

#!/usr/bin/env python #-*-coding:utf-8-*-import httplib, urllib, urllib2 import time import sys,os import re import J Son username = ' xxxx ' #账号 password = ' xxx ' #密码 format = ' json ' domain = [u ' www.youdomain.com '] #要解析的域名 def get_domain_i
  NFO (domain): domain_split = Domain.split ('. ') Domain_split_len = Len (domain_split) Maindomain = domain_split[domain_split_len-2] + '. ' + domain_split[domain_split_l En-1] return Maindomain,domain params = {' Login_email ': username, ' Login_password ':p assword, ' format ': format} def reque St (Action, params, method = ' POST '): headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "Text/jso n "} conn = Httplib. Httpsconnection ("dnsapi.cn") conn.request (method, '/' + action, Urllib.urlencode (params), headers) response = Conn.get Response () data = Response.read () conn.close () if Response.Status = = 200:return data Else:return None D EF 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.G ET (' id ') return domaninfo def get_my_domain_record_id (domain_id): params[' domain_id '] = domain_id data = Request (' R Ecord. List ', params ' data = json.loads (data) if Data.get (' code ') = = ': return None domainname = data.get (' domain '). g ET (' name ') record_list = Data.get (' records ') record = {} for R in Record_list:if r.get (' type ') = = ' A ': ke y = r.get (' name ')!= ' @ ' and r.get (' name ') + '. ' + domainname or domainname Record[key] = {' id ': r.get (' id '), ' value ': R.get (' value ')} return record Def changerecord (DOMAIN,DOMAIN_ID,RECORD_ID,IP): params[' domain_id '] = domain_id para
  ms[' 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 = response.read () IP = re.findall (R ' \d+.\d+.\d+.\d+ ', text) return ip[0] or N One def updatedomaininfo (domain): m,sub_m = get_domain_info (domain) domain_id = my_domain_id_list.get (m) record_lis
  t = 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_id if __name__ = = ' __main__ ': my_domain_id_list = get_my_domain_id () try:for dm in domain:domaindata = Updatedom
      Aininfo (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

Related Article

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.