def UpdateDomainRecord(accessKeyId,accessSecret,NewIP): client = AcsClient(accessKeyId,accessSecret, 'default') request = CommonRequest() request.set_accept_format('json') request.set_domain('alidns.aliyuncs.com') request.set_method('POST') request.set_protocol_type('https') # https | http request.set_version('2015-01-09') request.set_action_name('UpdateDomainRecord') request.add_query_param('RR', 'WWW') request.add_query_param('Value', NewIP) request.add_query_param('Type', 'A') request.add_query_param('RecordId', '') response = str(client.do_action(request), encoding = 'utf-8') msg=json.loads(response) if 'Message' in msg: msg = msg['Message'] content = stamp_to_time() + ' ' + msg + '\n' WriteContent('log.conf', content) else : content = stamp_to_time() + ' ' + 'the ip of demain liuyaoze.cn has change to :'+NewIP + '\n' WriteContent('log.conf', content)
def get_all_regions(self): '''get all available reigins for current region :return: a list, which is mapping for regions ''' client = AcsClient(self.secret_key, self.secret_id, "default") request = self.__common_request_settings request.set_action_name('DescribeRegions') request.set_domain('ecs.aliyuncs.com') response = client.do_action_with_exception(request) str_response = (str(response, encoding='utf-8')) regions = json.loads(str_response).get('Regions').get('Region') return regions
def getRecordId(domain): client = AcsClientSing.getInstance() request = CommonRequestSing.getInstance() request.set_domain('alidns.aliyuncs.com') request.set_version('2015-01-09') request.set_action_name('DescribeDomainRecords') request.add_query_param( 'DomainName', Utils.getConfigJson().get('First-level-domain')) response = client.do_action_with_exception(request) jsonObj = json.loads(response.decode("UTF-8")) records = jsonObj["DomainRecords"]["Record"] for each in records: if each["RR"] == domain: return each["RecordId"]
def get_all_regions(self): '''get all available reigins for current region :return: a list, which is mapping for regions ''' try: client = AcsClient(self.secret_id, self.secret_key, "default") request = self.__common_request_settings request.set_action_name('DescribeRegions') request.set_domain('ecs.aliyuncs.com') response = client.do_action_with_exception(request) str_response = (str(response, encoding='utf-8')) regions = json.loads(str_response).get('Regions').get('Region') return regions except ServerException: return {"msg":_("InvalidAccessKeyId.NotFound Specified access key is not found")}
def get_token(): # 创建AcsClient实例 client = AcsClient("AccessKey ID", "AccessKeySecret", "cn-shanghai") # 创建request,并设置参数 request = CommonRequest() request.set_method('POST') request.set_domain('nls-meta.cn-shanghai.aliyuncs.com') request.set_version('2018-05-18') request.set_uri_pattern('/pop/2018-05-18/tokens') response = client.do_action_with_exception(request) response = json.loads(response.decode("utf-8")) print(response) # token_expireTime token的有效期(时间戳) access_token = response.get("Token").get("Id") token_expireTime = response.get("Token").get("ExpireTime") return access_token, token_expireTime
def alarm(message): print('开始发送信息:' + message) client = AcsClient(ACCESS_KEY_ID, ACCESS_SECRET, 'cn-hangzhou') request = CommonRequest() request.set_accept_format('json') request.set_domain('dysmsapi.aliyuncs.com') request.set_method('POST') request.set_protocol_type('https') request.set_version('2017-05-25') request.set_action_name('SendSms') request.add_query_param('RegionId', "cn-hangzhou") request.add_query_param('PhoneNumbers', PHONE_NUMBER) request.add_query_param('SignName', "KisPig网") request.add_query_param('TemplateCode', "SMS_184215625") request.add_query_param('TemplateParam', "{\"message\": \"" + message + "\"}") response = client.do_action_with_exception(request) print("通知短信%s已发送%s" % (message, response)) quit()
def checkDomian(): client = AcsClientSing.getInstance() request = CommonRequestSing.getInstance() request.set_domain('alidns.aliyuncs.com') request.set_version('2015-01-09') request.set_action_name('DescribeDomainRecords') request.add_query_param( 'DomainName', Utils.getConfigJson().get('First-level-domain')) response = client.do_action_with_exception(request) jsonObj = json.loads(response.decode("UTF-8")) records = jsonObj["DomainRecords"]["Record"] dnsrecord = [] for each in records: dnsrecord.append({ "{_RR}.{_domain}".format(_RR=each["RR"], _domain=each["DomainName"]): each["Value"] }) return dnsrecord
def get_region_instance_detail(self, region_id, region_endpoint): '''get instances from one special region :param region_id: a string,regionID,区域id ,egg:`cn-qingdao` :return: a list, all instances ''' client = AcsClient(self.secret_key, self.secret_id, region_id) request = self.__common_request_settings request.set_domain(region_endpoint) request.set_action_name("DescribeInstances") request.add_query_param('RegionId', region_id) try: response = client.do_action_with_exception(request) str_response = str(response, encoding="utf-8") result = json.loads(str_response).get('Instances').get('Instance') if result: self.instances.append(result) else: return except Exception: pass
def __test_msg(params): """测试短信接口 :return: 无返回值,测试结果通过控制台退出码获得。0为成功,非0为失败 """ params_config = { 'sms_interface_type': { 'must': True, 'data': True, 'short': 'I', 'long': 'interface' }, 'mobile': { 'must': True, 'data': True, 'short': 'M', 'long': 'mobile' }, 'ali_accesskeyid': { 'must': True, 'data': True, 'short': 'K', 'long': 'keyid' }, 'ali_accesssecret': { 'must': True, 'data': True, 'short': 'S', 'long': 'secret' }, 'ali_signname': { 'must': True, 'data': True, 'short': 'N', 'long': 'signname' }, 'ali_verify_templatecode': { 'must': True, 'data': True, 'short': 'T', 'long': 'template_id' }, } ret_dict = get(params_config, params) errcode = ret_dict.get('errcode') if errcode and errcode < 0: sys.exit(131) try: sms_interface_type = ret_dict.get('data').get('sms_interface_type') mobile = ret_dict.get('data').get('mobile') ali_accesskeyid = ret_dict.get('data').get('ali_accesskeyid') ali_accesssecret = ret_dict.get('data').get('ali_accesssecret') ali_signname = ret_dict.get('data').get('ali_signname') ali_verify_templatecode = ret_dict.get('data').get( 'ali_verify_templatecode') if sms_interface_type == "2": import urllib import urllib.request import hashlib statusStr = { '0': '短信发送成功', '-1': '参数不全', '-2': '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间', '30': '密码错误', '40': '账号不存在', '41': '余额不足', '42': '账户已过期', '43': 'IP地址限制', '50': '内容含有敏感词' } smsapi = "http://api.smsbao.com/" data = urllib.parse.urlencode({ 'u': ali_accesskeyid, 'p': ali_accesssecret, 'm': mobile, 'c': ali_signname }) send_url = smsapi + 'sms?' + data response = urllib.request.urlopen(send_url) the_page = response.read().decode('utf-8') if the_page != "0": print("\033[1;31m\n短信接口测试失败\n\033[0m") print(statusStr[the_page]) sys.exit(131) else: from aliyunsdkcore.client import AcsClient from aliyunsdkcore.request import CommonRequest client = AcsClient(ali_accesskeyid, ali_accesssecret, 'cn-hangzhou') request = CommonRequest() request.set_accept_format('json') request.set_domain('dysmsapi.aliyuncs.com') request.set_method('POST') request.set_protocol_type('https') # https | http request.set_version('2017-05-25') request.set_action_name('SendSms') request.add_query_param('RegionId', "cn-hangzhou") request.add_query_param('PhoneNumbers', mobile) request.add_query_param('SignName', ali_signname) request.add_query_param('TemplateCode', ali_verify_templatecode) request.add_query_param('TemplateParam', "{\"code\":8888}") response = client.do_action(request) result = str(response, encoding='utf-8') if result.find('"OK"') == -1: print("\033[1;31m\n短信接口测试失败\n\033[0m") print(result) sys.exit(131) print("\033[1;32m\n短信接口测试成功\n\033[0m") sys.exit(0) except Exception as e: print("\033[1;31m\n短信接口测试失败!\n\033[0m") print(e) sys.exit(131)