阿里云使用python实现DDNS系统

内容纲要

安装python SDK

pip install alibabacloud_alidns20150109==3.2.0

常用DNSapi调用

import json
from enum import unique, Enum

import SETTING
from alibabacloud_alidns20150109.client import Client as Alidns20150109Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_alidns20150109 import models as alidns_20150109_models
from alibabacloud_tea_util import models as util_models

@unique
class DomainType(Enum):
    A = "A"
    CNAME = "CNAME"
    MX = "MX"
    TXT = "TXT"
    NS = "NS"
    # 根据需要添加更多解析类型

class Sample:
    def __init__(self, access_key_id, access_key_secret):
        """
        使用AK&SK初始化账号Client
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        self.type = "ali"
        config = open_api_models.Config(
            # 必填,您的 AccessKey ID,
            access_key_id=access_key_id,
            # 必填,您的 AccessKey Secret,
            access_key_secret=access_key_secret
        )
        # Endpoint 请参考 https://api.aliyun.com/product/Alidns
        config.endpoint = f'alidns.cn-hangzhou.aliyuncs.com'
        self.client = Alidns20150109Client(config)

    # 获取当前时间
    @staticmethod
    def runTime():
        return util_models.RuntimeOptions()

    # 获取域名列表
    def getDescribeDomains(self):
        request = alidns_20150109_models.DescribeDomainsRequest()
        data = self.client.describe_domains_with_options(request, Sample.runTime())
        domains = []
        data_list = data.body.domains.domain
        for domain_info in data_list:
            domain_name = domain_info.domain_name
            domains.append(domain_name)
        return domains

    # 获取单个域名详情
    def getDescribeDomainInfo(self, domain_name):
        request = alidns_20150109_models.DescribeDomainInfoRequest(domain_name=domain_name)
        data = self.client.describe_domain_info_with_options(request, Sample.runTime())
        return data.body

    # 获取域名解析列表
    def getDescribeDomainRecords(self, domain_name):
        request = alidns_20150109_models.DescribeDomainRecordsRequest(domain_name=domain_name, page_size=500)
        data = self.client.describe_domain_records_with_options(request, Sample.runTime())
        record_list = data.body.domain_records.record
        domain_records = []
        for record in record_list:
            re_data = {
                'domain_name': record.domain_name,
                'rr': record.rr,
                'record_id': record.record_id,
                'type': record.type,
                'value': record.value,
                "domain_type": self.type
            }
            domain_records.append(re_data)
        return domain_records

    # 添加域名解析记录
    def addDomainRecord(self, domain_name, rr, d_type: DomainType | str, value):
        """

        :param domain_name: 域名
        :param rr: 解析记录
        :param d_type: 解析类型,[]
        :param value: 记录值
        :return:
        """
        if isinstance(d_type, DomainType):
            d_type = d_type.value
        request = alidns_20150109_models.AddDomainRecordRequest(
            domain_name=domain_name,
            rr=rr,
            type=d_type,
            value=value
        )
        data = self.client.add_domain_record_with_options(request, Sample.runTime())
        return data.body

    # 删除域名解析记录
    def deleteDomainRecord(self, record_id):
        """

        :param record_id: 添加记录时返回,或通过getDescribeDomainRecords获得
        :return:
        """
        request = alidns_20150109_models.DeleteDomainRecordRequest(
            record_id=record_id
        )
        data = self.client.delete_domain_record_with_options(request, Sample.runTime())
        return data.body

    # 删除域名解析记录(通过域名)
    def deleteSubDomainRecords(self, domain_name, rr):
        """

        :param domain_name: 域名
        :param rr: 记录
        :return:
        """
        request = alidns_20150109_models.DeleteSubDomainRecordsRequest(
            domain_name=domain_name,
            rr=rr
        )
        data = self.client.delete_sub_domain_records_with_options(request, Sample.runTime())
        return data.body

    # 修改域名解析记录
    def updateDomainRecord(self, record_id, rr, d_type: DomainType | str, value):
        """

        :param record_id: 添加记录时返回,或通过getDescribeDomainRecords获得
        :param rr: 解析记录
        :param d_type: 解析类型,[]
        :param value: 记录值
        :return:
        """
        if isinstance(d_type, DomainType):
            d_type = d_type.value
        request = alidns_20150109_models.UpdateDomainRecordRequest(
            record_id=record_id,
            rr=rr,
            type=d_type,
            value=value
        )
        data = self.client.update_domain_record_with_options(request, Sample.runTime())
        return data.body

更多api信息参考官方文档

阿里云官方文档

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容