用Python调用DNSPOD的API

     0评论

本文分享用Python操作DNSPOD域名API的脚本。

#coding: utf-8 import json import requests import argparse import copy URL_DLIST = "https://dnsapi.cn/Domain.List" URL_RADD = "https://dnsapi.cn/Record.Create" URL_RLIST = "https://dnsapi.cn/Record.List" URL_RUPDATE = "https://dnsapi.cn/Record.Modify" URL_RDELETE = "https://dnsapi.cn/Record.Remove" URL_ALLOWTYPE = "https://dnsapi.cn/Record.Type" TOKEN = "12345,3c2ycyagyqpndvcqw54u1hloyp848psg" COMMON_DATA = { "login_token": TOKEN, "format": "json" } PROXIES = {"http": "", "https": ""} def builddata(d): pdata = copy.deepcopy(COMMON_DATA) pdata.update(d) return pdata def check(r): if r.status_code != 200: print(r.status_code, r.text) raise def getallow(domain_grade="DP_Free"): pdata = builddata({ "domain_grade": domain_grade }) r = requests.post(URL_ALLOWTYPE, data=pdata, proxies=PROXIES) check(r) print("getallow\n" + r.text) # {"status":{"code":"1","message":"Action completed successful","created_at":"2018-10-24 16:11:42"},"types":["A","CNAME","MX","TXT","NS","AAAA","SRV","显性URL","隐性URL"]} def getdomains(): pdata = builddata({}) r = requests.post(URL_DLIST, data=pdata, proxies=PROXIES) check(r) print(r.text) def getdomain(domain): pdata = builddata({ "domain": domain }) r = requests.post(URL_RLIST, data=pdata, proxies=PROXIES) check(r) data = r.json() records = data["records"] for record in records: print("%s\t%s\t%s\t%s\t%s\t%s" % (record["id"], record["type"], record["name"], record["value"], record["ttl"], record["updated_on"])) return data def add_record(domain, recordtype, subdomain, value): pdata = builddata({ "domain": domain, "subdomain": subdomain, "recordtype": recordtype, "value": value }) r = requests.post(URL_RADD, data=pdata, proxies=PROXIES) check(r) data = r.json() print("add_record\n" + r.text) return data def update_record(domain, subdomain, recordtype, value): data = getdomain(domain) rid = None for record in data["records"]: if record['type'] == recordtype and record["name"] == subdomain: rid = record["id"] break if rid == None: print("no rid") return pdata = builddata({ "domain": domain, "record_id": rid, "sub_domain": subdomain, "record_type": recordtype, "value": value, "record_line": u"默认" }) print(pdata) r = requests.post(URL_RUPDATE, data=pdata, proxies=PROXIES) check(r) data = r.json() print("update_record\n" + r.text) return data def delete_record(domain, rid): pdata = builddata({ "domain": domain, "record_id": rid, }) r = requests.post(URL_RDELETE, data=pdata, proxies=PROXIES) check(r) data = r.json() print("delete_record\n" + r.text) return data def update_ssl(domain, value): update_record(domain, "_acme-challenge", "TXT", value) def main(args): if args.mode == "add": add_record(args.domain, args.subdomain, args.recordtype, args.value) elif args.mode == "update": update_record(args.domain, args.subdomain, args.recordtype, args.value) elif args.mode == "delete": delete_record(args.domain) elif args.mode == "list": getdomain(args.domain) elif args.mode == "ssl": update_ssl(args.domain, args.value) if __name__ == '__main__': argParser = argparse.ArgumentParser() argParser.add_argument('-m', '--mode', dest='mode', type=str, help='mode', choices=['add', 'delete', 'update', 'list', 'ssl']) argParser.add_argument('-t', '--recordtype', dest='recordtype', type=str, help='recordtype', choices=['A', 'CNAME', 'MX', 'TXT']) argParser.add_argument('-d', '--domain', dest='domain', type=str, help='domain') argParser.add_argument('-n', '--subdomain', dest='subdomain', type=str, help='subdomain') argParser.add_argument('-i', '--id', dest='id', type=str, help='id') argParser.add_argument('-v', '--value', dest='value', type=str, help='value') args = argParser.parse_args() main(args)

-- EOF --

本文最后修改于5年前 (2019-06-04)

差评不太行一般挺好非常不错 (No Ratings Yet)
读取中...
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址