Closed sunsheho closed 4 years ago
''' DDNS 主程序 使用阿里云的SDK发起请求 Created By Martin Huang on 2018/5/20 修改记录: 2018/5/20 => 第一版本 2018/5/26 => 增加异常处理、Requst使用单例模式,略有优化 2018/5/29 => 增加网络连通性检测,只有联通时才进行操作,否则等待 2018/6/10 => 使用配置文件存储配置,避免代码内部修改(需要注意Python模块相互引用问题) 2018/9/24 => 修改失败提示信息 ''' from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkcore.acs_exception.exceptions import ClientException from Utils import Utils import time,datetime import argparse from time import sleep import gc
def senderror(errcont): enow=datetime.datetime.now() now=enow.strftime('%Y-%m-%d %H:%M:%S') errfile=open('DDNS.log','a') errfile.write(now) errfile.write(str(errcont)) errfile.write('\n') errfile.close()
def DDNS(use_v6): client = Utils.getAcsClient() recordId = Utils.getRecordId(Utils.getConfigJson().get('Second-level-domain')) if use_v6: ip = Utils.getRealIPv6() type = 'AAAA' else: ip = Utils.getRealIP() type = 'A' iip = ({'type':type,'ip':ip}) print(iip) senderror(iip)
request = Utils.getCommonRequest() request.set_domain('alidns.aliyuncs.com') request.set_version('2015-01-09') request.set_action_name('UpdateDomainRecord') request.add_query_param('RecordId', recordId) request.add_query_param('RR', Utils.getConfigJson().get('Second-level-domain')) request.add_query_param('Type', type) request.add_query_param('Value', ip) response = client.do_action_with_exception(request) return response
def run(): while True: parser = argparse.ArgumentParser(description='DDNS') parser.add_argument('-6', '--ipv6', nargs='*', default=False) args = parser.parse_args() isipv6 = isinstance(args.ipv6, list)
try: while not Utils.isOnline(): time.sleep(6) continue result = DDNS(isipv6) # print("IP成功刷新!") senderror("IP成功刷新!") except (ServerException,ClientException) as reason: # print("IP刷新失败!原因是") # print(reason.get_error_msg()) senderror(reason.get_error_msg()) # print('-----------------------------------') nextcheck=0 reload=10 while nextcheck < int(reload): # print('距离下次检测还剩',int(reload)-nextcheck,'分钟') sleep(60) nextcheck=nextcheck+1 gc.collect()
if name == "main": while True: try: run() except: time.sleep(120) run()
''' DDNS 主程序 使用阿里云的SDK发起请求 Created By Martin Huang on 2018/5/20 修改记录: 2018/5/20 => 第一版本 2018/5/26 => 增加异常处理、Requst使用单例模式,略有优化 2018/5/29 => 增加网络连通性检测,只有联通时才进行操作,否则等待 2018/6/10 => 使用配置文件存储配置,避免代码内部修改(需要注意Python模块相互引用问题) 2018/9/24 => 修改失败提示信息 ''' from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkcore.acs_exception.exceptions import ClientException from Utils import Utils import time,datetime import argparse from time import sleep import gc
def senderror(errcont): enow=datetime.datetime.now() now=enow.strftime('%Y-%m-%d %H:%M:%S') errfile=open('DDNS.log','a') errfile.write(now) errfile.write(str(errcont)) errfile.write('\n') errfile.close()
def DDNS(use_v6): client = Utils.getAcsClient() recordId = Utils.getRecordId(Utils.getConfigJson().get('Second-level-domain')) if use_v6: ip = Utils.getRealIPv6() type = 'AAAA' else: ip = Utils.getRealIP() type = 'A' iip = ({'type':type,'ip':ip}) print(iip) senderror(iip)
def run(): while True: parser = argparse.ArgumentParser(description='DDNS') parser.add_argument('-6', '--ipv6', nargs='*', default=False) args = parser.parse_args() isipv6 = isinstance(args.ipv6, list)
if name == "main": while True: try: run() except: time.sleep(120) run()