1.代码部分
通过
ip = load(urlopen('https://api.ipify.org/?format=json'))['ip']
获得公网ip
通过阿里云提供的api获得域名对应的ip
两者不一致就进行替换
import json from urllib.request import urlopen from json import load from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest from aliyunsdkcore.client import AcsClient from aliyunsdkcore.auth.credentials import AccessKeyCredential from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest #获取本机公网ip ip = load(urlopen('https://api.ipify.org/?format=json'))['ip'] #accessKeyId,accessKeySecret是通过阿里云账户AccessKey管理获得的,以字符串的方式填入即可,region_id的查找站内有贴子不再赘述 credentials = AccessKeyCredential(accessKeyId, accessKeySecret) client = AcsClient(region_id='cn-hangzhou', credential=credentials) request = DescribeDomainRecordsRequest() #域名的填写要去掉www,比如www.aliyun.cn就填写'aliyun.cn' request.set_DomainName('aliyun.cn') response = client.do_action_with_exception(request) response_dict = json.loads(response) # 提取 'Value' 值,即阿里云解析中存储的ip地址 value = response_dict['DomainRecords']['Record'][0]['Value'] # 对比本机公网ip和阿里云存储ip,不一致则进行更新 if value!=ip: request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_RecordId("768707280628106240") request.set_RR("www") request.set_Type("A") request.set_Value(ip) response = client.do_action_with_exception(request) print(str(response, encoding='utf-8'))
2.设置定时任务
用win+r打开运行窗口,输入taskschd.msc打开定时任务创建界面,右侧选择创建任务
常规界面填写自己的任务名称
触发器界面新建触发器,选择一次,重复任务间隔选择5分钟,持续时间无限期
操作界面新建操作,程序选择自己python位置,参数选择自己脚本的绝对路径
确认即可使用
标签:set,Windows,ip,request,json,域名,import,response From: https://blog.csdn.net/m0_57631384/article/details/140128571