Python scripts Enable DNS dynamic resolution of DNSPod domain names and pythondnspod

Source: Internet
Author: User
Tags in domain

Python scripts Enable DNS dynamic resolution of DNSPod domain names and pythondnspod

In my spare time, I had a self-built server at home, because the residential broadband and IP addresses were dynamically allocated. Domain name resolution is a problem. My domain name is usually parked under DNSPod. DNSPod provides an API for modification, so it simply implements dynamic parsing using Python. In this way, there is no need to install a peanut shell. If you don't talk nonsense, read the code:

#! /Usr/bin/env python #-*-coding: UTF-8-*-import httplib, urllib, urllib2import timeimport sys, osimport reimport jsonusername = 'xxxx' # account password = 'xxx' # password format = 'json' domain = [u'www .youdomain.com '] # The domain name to be resolved 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] r Eturn 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 () co Nn. 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. loa Ds (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'] = 'default '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 = domains (domain_id) if record_list = None: return None rocord_info = record_list.get (sub_m) record_ip = values ('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) Doesn't: 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.