下面Python代码中的AcsClient()、request.set_RecordId("17741030***760")这两个函数的参数请在https://help.aliyun.com/document_detail/53090.html 上查看。
Code: [全选] [Expand/Collapse]
- #!/usr/bin/env python
- #coding=utf-8
- import json
- import urllib
- import datetime
- from aliyunsdkcore.client import AcsClient
- from aliyunsdkcore.acs_exception.exceptions import ClientException
- from aliyunsdkcore.acs_exception.exceptions import ServerException
- from aliyunsdkalidns.request.v20150109.DescribeDomainRecordInfoRequest import DescribeDomainRecordInfoRequest
- from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
- client = AcsClient('LTAIjOE53h******fI', 'JlvkmaCPek3SqqY*******GQbd8Zp', 'cn-hangzhou')
- request = DescribeDomainRecordInfoRequest()
- request.set_accept_format('json')
- request.set_RecordId("17741030***760")
- response = client.do_action_with_exception(request)
- response = json.loads(response.decode())
- old_ip = response['Value']
- new_ip = urllib.urlopen("http://ip.42.pl/raw").read()
- if old_ip != new_ip:
- request = UpdateDomainRecordRequest()
- request.set_accept_format('json')
- request.set_Value(new_ip)
- request.set_Type("A")
- request.set_RR("nas")
- request.set_RecordId("17741030***760")
- response = client.do_action_with_exception(request)
- print datetime.datetime.now(),' ', old_ip, ' => ', new_ip
- else :
- print datetime.datetime.now(),' IP has not changed.', new_ip