10-14 5 views
1. 安装阿里云云解析管理模块
参考:https://develop.aliyun.com/tools/sdk/?spm=a2c4g.11186623.2.9.249c37b4WNVPv8#/python
1 |
pip install aliyun-python-sdk-alidns |
2. 接口使用
https://help.aliyun.com/document_detail/29776.html?spm=a2c4g.11186623.6.638.77753b59Hc21Z0
3. 代码
以下为个人在项目中的应用,仅供参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author : Eric Winn # @Email : eng.eric.winn@gmail.com # @Time : 2019/10/13 11:43 PM # @Version : 1.0 # @File : records # @Software : PyCharm import json from django.conf import settings from aliyunsdkcore.client import AcsClient from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest from libs.utils import get_logger logger = get_logger(__file__) def print_dict_key(item, key): region_id = item.get(key) return region_id class Record: ''' 获取阿里云账户下所有的域名的解析信息 参考文档:https://help.aliyun.com/document_detail/29776.html?spm=a2c4g.11186623.3.3.5a543b59RyjdAD 如果传入access_key_id和access_key_secret则使用传入的access key建立连接 返回一个域名的所有解析记录 ''' def __init__(self, access_key_id=None, access_key_secret=None): self.access_key = access_key_id if access_key_id else settings.ALIYUN_ACCESS_KEY_ID self.secret = access_key_secret if access_key_secret else settings.ALIYUN_ACCESS_KEY_SECRET self.client = AcsClient(self.access_key, self.secret, "cn-hangzhou") self.currentPage = [] self.TotalPageNum = 0 self.PageSize = 500 def __do_action(self, request): try: response = self.client.do_action_with_exception(request) except Exception as e: logger.error(e) return return json.loads(str(response, encoding='utf-8')) def __get_total_page_num(self, domainName, PageNum=1, PageSize=1): ''' 获取解析记录页数 :param domainName: 域名 :return: ''' request = DescribeDomainRecordsRequest() request.set_DomainName(domainName) request.set_PageNumber(PageNum) request.set_PageSize(PageSize) response = self.__do_action(request) if self.TotalPageNum != 0: return response['DomainRecords']['Record'] else: if int(response['TotalCount']) % self.PageSize != 0: self.TotalPageNum = int(response['TotalCount'] / self.PageSize) + 1 else: self.TotalPageNum = int(response['TotalCount'] / self.PageSize) return self.TotalPageNum def get_records(self, domainName): ''' 获取解析记录 :param domainName: :return: 本域名下所有的解析信息 ''' self.TotalPageNum = 0 self.PageSize = 100 records_list = [] self.__get_total_page_num(domainName) for p in range(1, self.TotalPageNum + 1): records_list.extend(self.__get_total_page_num(domainName, p, self.PageSize)) return records_list |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!
