11-13 10 views
因DnsPod已合并到腾讯云,此前在DnsPod上管理的域名部分升级后已无法再继续在DnsPod上去管理,无奈之下引入腾讯云接口
这里只是拿域名解析的模块做测试,如果要管理其它产品,可以修改”module”,调用对应的action就可以了
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 |
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 引入云API入口模块 from django.conf import settings import base64 import hashlib import hmac import random import time import operator import json import urllib.parse import urllib3 from .utils import get_logger logger = get_logger(__file__) urllib3.disable_warnings() class QcloudApi(): def __init__(self): self.SecretId = 'xxxxxxxxxxxx' self.secretKey = 'xxxxxxxxxxxx' def get(self, module, action, **params): config = { 'Action': action, 'Nonce': random.randint(10000, 99999), 'SecretId': self.SecretId, 'SignatureMethod': 'HmacSHA256', 'Timestamp': int(time.time()), } url_base = '{0}.api.qcloud.com/v2/index.php?'.format(module) params_all = dict(config, **params) params_sorted = sorted(params_all.items(), key=operator.itemgetter(0)) srcStr = 'GET{0}'.format(url_base) + ''.join("%s=%s&" % (k, v) for k, v in dict(params_sorted).items())[:-1] signStr = base64.b64encode(hmac.new(bytes(self.secretKey, encoding='utf-8'), bytes(srcStr, encoding='utf-8'), digestmod=hashlib.sha256).digest()).decode('utf-8') config['Signature'] = signStr params_last = dict(config, **params) params_url = urllib.parse.urlencode(params_last) url = 'https://{0}&'.format(url_base) + params_url http = urllib3.PoolManager() r = http.request('GET', url=url, retries=False) ret = json.loads(r.data.decode('utf-8')) if ret.get('code', {}) == 0: return ret else: raise Exception(ret) #调用例子: qcloud = QcloudApi() # ret = qcloud.get(module='cns', action='DomainCreate', domain='abc.com') # ret = qcloud.get(module='cns', action='SetDomainStatus', domain='abc.com', status='enable') # 'enable' or 'disable' ret = qcloud.get(module='cns', action='DomainList') # ret = qcloud.get(module='cns', action='DomainDelete', domain='abc.com') # ret = qcloud.get(module='cns', action='RecordList', domain='abc.com', length=100) # ret = qcloud.get(module='cns', action='RecordCreate', domain='abc.com', subDomain='www', value='192.168.100.11', recordType='A', recordLine='联通') # ret = qcloud.get(module='cns', action='RecordModify', domain='abc.com', recordId=388853153, subDomain='www', value='192.168.101.11', recordType='A', recordLine='电信', remark='主站搜索引擎线路') # ret = qcloud.get(module='cns', action='RecordStatus', domain='abc.com', recordId=388745814, status='enable') # ret = qcloud.get(module='cns', action='RecordDelete', domain='abc.com', recordId=388745814) print(ret['data']['domains'][0].get('name')) if 'coinlab.club' in [item['name'] for item in ret['data']['domains']]: print('yes') |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!