python aliyun myddns

#!/usr/bin/env python
#coding=utf-8

"""
新增域名解析记录,参数说明如下:
<accessKeyId>:填写自己的accessKey,建议使用RAM角色管理的Key
<accessSecret>:填写自己的accessSecret,建议使用RAM角色管理的Secret

"""

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from leokim_function import *
from check_cnnect_network import *
import os
import time


while True:
    #判断存放IP的文件是否存在,不存在则创建
    if os.path.exists("./ip"):
        pass
    else:
        wirte_to_file("./ip","0.0.0.0")

    client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')

    if(connect_code() == 200):
        #通过函数获取外网ip
        ip = get_internet_ip()
        #print(ip)
    else:
        time.sleep(5)
        continue

    #下面开始对比ip,如果ip与之前记录的ip一致,则不执行任何操作,如果ip有变化,则会更新本地存储文件和更新域名解析
    with open("./ip", 'r') as f:
        old_ip = f.read()
    if ip == old_ip:
        print("noupdate"+"\nnew_ip:"+ip+"\nold_ip:"+old_ip)
    else:
        #print("update"+"\nnew_ip:"+ip+"\nold_ip:"+old_ip)
        wirte_to_file("./ip",ip)
        des_relsult = Describe_SubDomain_Records(client,"A","rpi.sgl1885.com")
                #判断子域名解析记录查询结果,TotalCount为0表示不存在这个子域名的解析记录,需要新增一个
        if des_relsult["TotalCount"] == 0:
            add_relsult = add_record(client,"5","600","A",ip,"rpi","sgl1885.com")
            record_id = add_relsult["RecordId"]
            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,"5","600","A",ip,"rpi",record_id)
            print("域名解析更新成功!")
        else:
            record_id = 0
            print("存在两个子域名解析记录值,请核查删除后再操作!")
        path = './RecordId'
        wirte_to_file(path,record_id)
    time.sleep(10)

#!/usr/bin/env python
#coding=utf-8


"""
Priority(5):解析优先级,非必填
TTL(600):TTL值,默认600秒,非必填
Value("121.201.65.98"):记录值,必填
request.set_Type("A"):解析类型,A为解析成IPv4,如需解析根域名,填写@,必填
request.set_RR("www"):子域名,必填
request.set_DomainName("xiaoanran.club"):根域名,必填

返回值说明:
RecordId:解析记录ID,修改和查询域名时需要用到
RequestId:请求ID

"""
from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
from aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest
import urllib
import json

#写入文件
def wirte_to_file(path,content):
    with open(path,'w') as f:
        f_name = open(path,'w')
        f_name.write(content)

#新增解析记录,返回json格式的数据
def 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 relsult

#更新解析记录
def update_record(client,priority,ttl,record_type,value,rr,record_id):
    request = UpdateDomainRecordRequest()
    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_RecordId(record_id)

    response = client.do_action_with_exception(request)
    #response = str(response, encoding='utf-8')
    return response

#查询解析记录
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 relsult

#获取外网地址
def get_internet_ip():
    response = urllib.urlopen('http://www.3322.org/dyndns/getip')
    ip = response.read()
    return ip