腾讯qcloud域名解析api

由于特殊原因,购买一台阿里云的`按流量付费`的vps

比如早9点到晚9点使用,其余时间`关机不收费`

写了脚本每次开机会自动邮件告诉我服务器的ip,省去了打开阿里云后台看ip的麻烦

但是每次还要我手工绑定ip

因为我的域名是在腾讯云上购买的,所以想到有没有办法域名自动解析到指定的ip地址,于是百度打开相关的api

以下脚本是`基于python3`的,推荐使用`centos8`,默认安装`python3`

>[root@iZ6weck4rpeui7m2kjuq9bZ ~]# cat parsedomain.py
import base64
import hashlib
import hmac
import json
import random
from datetime import datetime
from urllib.parse import urlencode
from urllib.request import urlopen
from json import load
my_ip = load(urlopen('http://httpbin.org/ip'))['origin']
host = 'cns.api.qcloud.com/v2/index.php'
secret_id = '你自己的secret_id'
secret_key = '你自己的secret_key'
def request(action, region=None, dict_arg=None, kw_arg):
params = dict(dict_arg) if dict_arg is not None else {}
params.update(kw_arg)
params['Action'] = action
if region is not None:
params['Region'] = region
params['Timestamp'] = int(datetime.timestamp(datetime.now()))
params['Nonce'] = random.randint(1, 2 16 - 1)
params['SecretId'] = secret_id
params['SignatureMethod'] = 'HmacSHA256'
params = {str(k): str(v) for k, v in params.items()}
text = 'GET' + host + '?' + '&'.join(k + '=' + v for k, v in sorted(params.items()))
signature = hmac.new(secret_key.encode(), text.encode(), hashlib.sha256).digest()
params['Signature'] = base64.b64encode(signature).decode()
url = 'https://' + host + '?' + urlencode(sorted(params.items()))
contents = json.loads(urlopen(url).read().decode('unicode-escape'))
if contents['code'] != 0:
raise Exception(contents['message'])
return contents['data']
records = request('RecordList', domain="你自己的主域名不带www.")['records']
for record in records:
if record['name'] == '子域名名称不包含主域名':
if record['value'] != my_ip:
request('RecordModify',
domain="你自己的主域名不带www.",
recordId=record['id'],
subDomain=record['name'],
recordType=record['type'],
recordLine=record['line'],
value=my_ip,
ttl=record['ttl'],
mx=record['mx'])

申明以上脚本大部分参考[https://i-m.dev/posts/20190223-214629.html](https://i-m.dev/posts/20190223-214629.html)

    A+
发布日期:2020年11月01日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: