-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnspodApi.py
66 lines (58 loc) · 3.14 KB
/
dnspodApi.py
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
# -*- encoding:utf-8 -*-
import time
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.dnspod.v20210323 import dnspod_client, models
def change_dns(SecretId, SecretKey, Domain, SuDomain, RecordType, RecordId: int, Value, TTL=None):
r"""修改dns记录
:param SecretId:
:param SecretKey:
:param Domain: 域名
:param SuDomain: 主机记录
:param RecordType: 记录类型
:param RecordId: 记录Id
:param Value: 记录值
:param TTL: TTL值
:return: 操作结果
"""
for i in range(5):
try:
# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
# 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
cred = credential.Credential(SecretId, SecretKey)
# 实例化一个http选项,可选的,没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "dnspod.tencentcloudapi.com"
# 实例化一个client选项,可选的,没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = dnspod_client.DnspodClient(cred, "", clientProfile)
# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.ModifyRecordRequest()
params = {
"Domain": Domain, # 域名
"DomainId": None, # 域名id,优先级高于域名
"SubDomain": SuDomain, # 主机记录
"RecordType": RecordType, # 记录类型
"RecordLine": "默认", # 记录线路
"RecordLineId": None, # 记录线路id,优先级高于记录线路
"Value": Value, # 记录值,IP
"MX": None, # MX优先级
"TTL": TTL, # TTL 0-604800
"Status": None, # 记录状态
"RecordId": RecordId # 记录id
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个ModifyRecordResponse的实例,与请求对象对应
resp = client.ModifyRecord(req)
# 输出json格式的字符串回包
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}]" + "DNS修改成功")
return True
except TencentCloudSDKException as err:
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}]第{i + 1}次请求失败\n错误:{err}")
return False