实现一个简单的dnspod解析api:


dnspod api地址:https://www.dnspod.cn/docs/records.html#record-create


#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chenmingle'

import urllib2
import urllib
import json
import time
import socket


public_dic={}
public_dic["login_token"]=("%s,%s" % ('access_id','access_key'))
public_dic["format"]="json"
headers={}
headers["User-Agent"]="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"


def getDomainID(domain):
    url="https://dnsapi.cn/Domain.INFO"
    info=public_dic.copy()
    info["domain"]=domain
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    formatJson=json.load(response)
#    print formatJson["domain"]['id']
#    print formatJson["domain"]
    if formatJson["status"]["code"]!="1":
        return 0
    else:
        return formatJson["domain"]["id"]
    pass
    
    
def getRecordList(domain_id):
    url="https://dnsapi.cn/Record.List"
    info=public_dic.copy()
    info["domain_id"]=domain_id
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
#    print dnsJson
    for i in dnsJson['records']:
        print i['id'],i['type'],i['name'],i['line'],i['line_id'],i['value'],i['ttl'],i['enabled']
        
        
def getRecordID(domain_id,record,line_id):
    url="https://dnsapi.cn/Record.List"
    info=public_dic.copy()
    info["domain_id"]=domain_id
    info["sub_domain"]=record
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
#    print dnsJson
    for i in dnsJson['records']:
        if i['line_id'] == line_id:
            print i['id'],i['type'],i['name'],i['line'],i['value'],i['ttl'],i['status']
            
            
def createRecord(domain_id,sub_domain,record_type,record_line_id,value):
    url="https://dnsapi.cn/Record.Create"
    info=public_dic.copy()
    info['domain_id']=domain_id
    info['sub_domain']=sub_domain
    info['record_type']=record_type
    info['record_line_id']=record_line_id
    info['value']=value
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    print dnsJson
    
    
def updateRecord(domain_id,sub_domain,record_type,record_line_id,value):
    url="https://dnsapi.cn/Record.List"
    info=public_dic.copy()
    info["domain_id"]=domain_id
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    for i in dnsJson['records']:
        if i['name'] == sub_domain and i['line_id'] == record_line_id:
        record_id=i['id']
        
        
print record_id
    url="https://dnsapi.cn/Record.Modify"
    info=public_dic.copy()
    info['domain_id']=domain_id
    info['record_id']=record_id
    info['sub_domain']=sub_domain
    info['record_type']=record_type
    info['record_line_id']=record_line_id
    info['value']=value
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    print dnsJson
    
    
def updateRecordLine(domain_id,sub_domain,record_type,line_id,record_line_id,value):
    url="https://dnsapi.cn/Record.List"
    info=public_dic.copy()
    info["domain_id"]=domain_id
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    for i in dnsJson['records']:
        if i['name'] == sub_domain and i['line_id'] == line_id:
                record_id=i['id']
                print record_id
    url="https://dnsapi.cn/Record.Modify"
    info=public_dic.copy()
    info['domain_id']=domain_id
    info['record_id']=record_id
    info['sub_domain']=sub_domain
    info['record_type']=record_type
    info['record_line_id']=record_line_id
    info['value']=value
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    print dnsJson
    
    
def statusRecord(domain_id,sub_domain,record_line_id,status):
    url="https://dnsapi.cn/Record.List"
    info=public_dic.copy()
    info["domain_id"]=domain_id
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    for i in dnsJson['records']:
        if i['name'] == sub_domain and i['line_id'] == record_line_id:
                record_id=i['id']
    url="https://dnsapi.cn/Record.Status"
    info=public_dic.copy()
    info['domain_id']=domain_id
    info['record_id']=record_id
    info['status']=status
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    print dnsJson
    
    
def deleteRecord(domain_id,sub_domain,record_line_id):
    url="https://dnsapi.cn/Record.List"
    info=public_dic.copy()
    info["domain_id"]=domain_id
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    for i in dnsJson['records']:
        if i['name'] == sub_domain and i['line_id'] == record_line_id:
                record_id=i['id']
    url="https://dnsapi.cn/Record.Remove"
    info=public_dic.copy()
    info['domain_id']=domain_id
    info['record_id']=record_id
    request=urllib2.Request(url,headers=headers,data=urllib.urlencode(info))
    response=urllib2.urlopen(request)
    dnsJson=json.load(response)
    print dnsJson
    
    
id = getDomainID('xxx.com')
getRecordList(id)
#getRecordID(id,'cml','10=1')
#createRecord(id,'cml','A','0','*.*.*.*')
#updateRecordLine(id,'cml','A','10=1','10=1','*.*.*.*')
#statusRecord(id,'cml','10=1','enabled')
#deleteRecord(id,'cml','10=1')