一、配置config.json二、代码三、打包执行
思路:阿里云提供dns域名解析api,查出外网ip,定时更新域名解析。
一、配置config.json
用配置文件,动态配置二级域名,ip,accessKey等。
{ "ip": "223.76.234.68", "accessKeyId": "", //阿里云控制台生成 "accessSecret": "", "domain": "xxx.com.cn", //一级域名 "subDomain": "test", //二级域名前缀,多个用英文逗号隔开 "time": "6" //定时更新,秒}
二、代码python3依赖:
pip install requestspip install aliyun-python-sdk-alidnspip install aliyun-python-sdk-corepip install aliyun-python-sdk-domain
代码:
import jsonimport timeimport requestsfrom aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequestfrom aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequestfrom aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequestfrom aliyunsdkcore.client import AcsClient#获取外网ipdef get_internet_ip(): with requests.get('http://www.3322.org/dyndns/getip') as response: html = response.content ip = str(html, encoding='utf-8').replace("n", "") return ip#记录上次外网ipdef wirte_to_config(data): with open("./config.json", "w") as jsonFile: json.dump(data, jsonFile, ensure_ascii=False)#读取第一步配置def get_config(): with open('./config.json', 'r', encoding='utf8')as fp: return json.load(fp) #获取二级域名信息def Describe_SubDomain_Records(client, record_type, subdomain): request = DescribeSubDomainRecordsRequest() request.set_accept_format('json') request.set_Type(record_type) request.set_SubDomain(subdomain) response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') relsult = json.loads(response) return relsultdef add_record(client, priority, ttl, record_type, value, rr, domainname): request = AddDomainRecordRequest() request.set_accept_format('json') request.set_Priority(priority) request.set_TTL(ttl) request.set_Value(value) request.set_Type(record_type) request.set_RR(rr) request.set_DomainName(domainname) response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') relsult = json.loads(response) return relsultdef update_record(client, ttl, record_type, value, rr, record_id): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_TTL(ttl) request.set_Value(value) request.set_Type(record_type) request.set_RR(rr) request.set_RecordId(record_id) response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') return responsedef deal_aliyun_domain(client, domain, subs, ip): for sub in subs: subDomain = sub + '.' + domain des_relsult = Describe_SubDomain_Records(client, "A", subDomain) if des_relsult["TotalCount"] == 0: add_record(client, "5", "600", "A", ip, sub, domain) print("域名解析新增成功!") # 判断子域名解析记录查询结果,TotalCount为1表示存在这个子域名的解析记录,需要更新解析记录,更新记录需要用到RecordId,这个在查询函数中有返回des_relsult["DomainRecords"]["Record"][0]["RecordId"] elif des_relsult["TotalCount"] == 1: record_id = des_relsult["DomainRecords"]["Record"][0]["RecordId"] update_record(client, "600", "A", ip, sub, record_id) print("域名解析更新成功!") else: print("存在两个子域名解析记录值,请核查删除后再操作!")def run(c): ip = get_internet_ip() old_ip = c['ip'] if ip == old_ip: print("noupdate" + "nnew_ip:" + ip + "nold_ip:" + old_ip) else: c['ip'] = ip wirte_to_config(c) client = AcsClient(c['accessKeyId'], c['accessSecret'], 'cn-hangzhou') domain = c['domain'] subs = c['subDomain'].split(',') deal_aliyun_domain(client, domain, subs, ip)if __name__ == "__main__": while True: c = get_config() time.sleep(int(c['time'])) run(c)
三、打包执行 window服务器
安装Pyinstaller,在项目目录中执行
Pyinstaller -D -w main.py
并将配置文件放到dist/main目录中,单击mian.exe即可(可能会被杀毒 软件杀掉),
参考引用:https://developer.aliyun.com/article/702552?spm=a2c6h.13813017.content3.1.34115cb6F8LFYY