Beispiel #1
0
 def create_record(self, domain: str, sub_domain: str, value: list,
                   record_type: str, line: str, ttl: int):
     zone_id = self.get_zone_id(domain)
     if zone_id != "The domain doesn't exist":
         url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets'
     else:
         return "The domain doesn't exist"
     if line == '电信':
         line = 'Dianxin'
     elif line == '联通':
         line = 'Liantong'
     elif line == '移动':
         line = 'Yidong'
     ips = []
     ips.append(value)
     data = {
         "line": line,
         "name": sub_domain + '.' + domain,
         "records": value,
         "ttl": ttl,
         "type": record_type
     }
     r = signer.HttpRequest('POST', url, body=json.dumps(data))
     self.sign.Sign(r)
     res = json.loads(
         requests.post(url, headers=r.headers, data=r.body).text)
     # print(res)
     # print(res['status'])
     try:
         if res['status'] == 'PENDING_CREATE':
             return 'success'
     except:
         return res
def _image_batch_jobs_aksk(sig, urls, categories=['politics', 'terrorism']):
    _url = 'https://%s/v1.0/moderation/image/batch/jobs' % ais.AisEndpoint.MODERATION_ENDPOINT

    _data = {"urls": urls, "categories": categories}

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = ais.AisEndpoint.MODERATION_ENDPOINT
    kreq.uri = "/v1.0/moderation/image/batch/jobs"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
        r = urllib2.urlopen(req, context=_context)
    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError, e:
        resp = e.read()
        status_code = e.code
Beispiel #3
0
 def get_record(self, domain: str, length: int, sub_domain: str,
                record_type: str):
     zone_id = self.get_zone_id(domain)
     if zone_id != "The domain doesn't exist":
         url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets?limit=' + str(
             length)
     else:
         return "The domain doesn't exist"
     r = signer.HttpRequest('GET', url)
     self.sign.Sign(r)
     res = json.loads(requests.get(url, headers=r.headers).text)
     records = []
     recordset_id = ''
     # print(res)
     try:
         for i in range(0, len(res['recordsets'])):
             if res['recordsets'][i]['name'].split(
                     '.')[0] == sub_domain and res['recordsets'][i][
                         'type'] == record_type:
                 records = res['recordsets'][i]['records']
                 recordset_id = res['recordsets'][i]['id']
         if records and recordset_id != '':
             return records, recordset_id
         else:
             return "The sub domain doesn't exist"
     except:
         return res
Beispiel #4
0
def moderation_text_aksk(_ak,
                         _sk,
                         text,
                         type='content',
                         cattegories=[
                             "ad", "politics", "politics", "politics",
                             "contraband", "contraband"
                         ]):
    _url = 'https://ais.cn-north-1.myhuaweicloud.com/v1.0/moderation/text'

    sig = signer.Signer()
    sig.AppKey = _ak
    sig.AppSecret = _sk

    _data = {
        "categories": cattegories,
        "items": [{
            "text": text,
            "type": type
        }]
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = "ais.cn-north-1.myhuaweicloud.com"
    kreq.uri = "/v1.0/moderation/text"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib.request.Request(url=_url,
                                     data=kreq.body,
                                     headers=kreq.headers)
        r = urllib.request.urlopen(req, context=_context)
    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError as e:
        resp = e.read()
        status_code = e.code
    except URLError as e:
        resp = e.read()
        status_code = e.code
    else:
        status_code = r.code
        resp = r.read()
    return resp.decode('utf-8')
Beispiel #5
0
def asr_sentence_aksk(_ak,
                      _sk,
                      data,
                      url,
                      encode_type='wav',
                      sample_rate='8k'):
    _url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/voice/asr/sentence"

    sig = signer.Signer()
    sig.AppKey = _ak
    sig.AppSecret = _sk

    if data != '':
        data = data.decode("utf-8")

    _data = {
        "url": url,
        "data": data,
        "encode_type": encode_type,
        "sample_rate": sample_rate
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = "ais.cn-north-1.myhuaweicloud.com"
    kreq.uri = "/v1.0/voice/asr/sentence"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib.request.Request(url=_url,
                                     data=bytes(kreq.body),
                                     headers=kreq.headers)
        r = urllib.request.urlopen(req, context=_context)
    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError as e:
        resp = e.read()
        status_code = e.code
    except URLError as e:
        resp = e.read()
        status_code = e.code
    else:
        status_code = r.code
        resp = r.read()
    return resp.decode('utf-8')
Beispiel #6
0
def _long_sentence_aksk(sig, data, url):
    _url = 'https://%s/v1.0/voice/asr/long-sentence' % ais.AisEndpoint.ASR_ENDPOINT

    _data = {"url": url, "data": data}

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = ais.AisEndpoint.ASR_ENDPOINT
    kreq.uri = "/v1.0/voice/asr/long-sentence"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
        r = urllib2.urlopen(req, context=_context)

    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError, e:
        resp = e.read()
        status_code = e.code
Beispiel #7
0
def _get_result_aksk(sig, job_id):
    _url_tmpl = 'https://ais.cn-north-1.myhuaweicloud.com/v1.0/voice/asr/long-sentence?job_id=%s'
    _url = _url_tmpl % job_id

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = "ais.cn-north-1.myhuaweicloud.com"
    kreq.uri = "/v1.0/voice/asr/long-sentence"
    kreq.method = "GET"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.query = {'job_id': job_id}
    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, headers=kreq.headers)
        r = urllib2.urlopen(req, context=_context)

        #
        # We use HTTPError and URLError,because urllib2 can't process the 4XX &
        # 500 error in the single urlopen function.
        #
        # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
        # there is no this problem.
        #
    except HTTPError, e:
        resp = e.read()
        status_code = e.code
Beispiel #8
0
def recapture_detect_aksk(_ak, _sk, image, url, threshold=0.95, scene=None):
    _url = 'https://ais.cn-north-1.myhuaweicloud.com/v1.0/image/recapture-detect'

    sig = signer.Signer()
    sig.AppKey = _ak
    sig.AppSecret = _sk

    if image != '':
        image = image.decode('utf-8')

    _data = {
        "image": image,
        "url": url,
        "threshold": threshold,
        "scene": scene,
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = "ais.cn-north-1.myhuaweicloud.com"
    kreq.uri = "/v1.0/image/recapture-detect"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib.request.Request(url=_url,
                                     data=kreq.body,
                                     headers=kreq.headers)
        r = urllib.request.urlopen(req, context=_context)

    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError as e:
        resp = e.read()
        status_code = e.code
    except URLError as e:
        resp = e.read()
        status_code = e.code
    else:
        status_code = r.code
        resp = r.read()
    return resp.decode('utf-8')
Beispiel #9
0
def moderation_text_aksk(_ak,
                         _sk,
                         text,
                         type='content',
                         categories=[
                             "ad", "politics", "politics", "politics",
                             "contraband", "contraband"
                         ]):
    _url = 'https://%s/v1.0/moderation/text' % ais.AisEndpoint.MODERATION_ENDPOINT

    sig = signer.Signer()
    sig.AppKey = _ak
    sig.AppSecret = _sk

    _data = {
        "categories":
        categories,  # 检测场景 Array politics:涉政 p**n:涉黄 ad:广告 abuse:辱骂 contraband:违禁品 flood:灌水
        "items": [{
            "text": text,
            "type": type
        }  # items: 待检测的文本列表  text 待检测文本 type 文本类型
                  ]
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = ais.AisEndpoint.MODERATION_ENDPOINT
    kreq.uri = "/v1.0/moderation/text"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
        r = urllib2.urlopen(req, context=_context)
    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError, e:
        resp = e.read()
        status_code = e.code
Beispiel #10
0
    def create_record(self, domain: str, sub_domain: str, value: list,
                      record_type: str, line: str, ttl: int):
        zone_id = self.get_zone_id(domain)
        if zone_id != "The domain doesn't exist":
            url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets'
        else:
            return "The domain doesn't exist"
        #if line == '电信':
        #    line = 'Dianxin'
        #elif line == '联通':
        #    line = 'Liantong'
        #elif line == '移动':
        #    line = 'Yidong'
        ips = []
        ips.append(value)
        data = {
            "line": line,
            "name": sub_domain + '.' + domain,
            "records": value,
            "ttl": ttl,
            "type": record_type
        }
        r = signer.HttpRequest('POST', url, body=json.dumps(data))
        self.sign.Sign(r)

        #替换为标准格式
        # {
        #   "result":True,
        #   "message":{...}
        # }
        resp = requests.post(url, headers=r.headers, data=r.body)
        try:
            resp.raise_for_status()
            res = json.loads(resp.text.decode('utf-8'))
        except HTTPError as httpError:
            response_status_code = httpError.response.status_code
            #response_header_params = httpError.response.headers
            #request_id = response_header_params["X-Request-Id"]
            #response_body = httpError.response.text
            return {
                "result": False,
                "message":
                "错误码:%s,错误描述:%s" % (response_status_code, response_body)
            }
        except:
            pass

        data = {}
        data["result"] = 'id' in ret
        data["message"] = res['status']
        if data["message"] == 'PENDING_DELETE':
            data["message"] = 'success'
        return data
Beispiel #11
0
def tts_aksk(_ak,
             _sk,
             text,
             voice_name='xiaoyan',
             volume='0',
             sample_rate='16k',
             speech_speed='0',
             pitch_rate='0'):
    _url = 'https://%s/v1.0/voice/tts' % ais.AisEndpoint.TTS_ENDPOINT

    sig = signer.Signer()
    sig.AppKey = _ak
    sig.AppSecret = _sk

    _data = {
        "text": text,
        "voice_name": voice_name,
        "volume": volume,
        "sample_rate": sample_rate,
        "speech_speed": speech_speed,
        "pitch_rate": pitch_rate
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = ais.AisEndpoint.TTS_ENDPOINT
    kreq.uri = "/v1.0/voice/tts"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
        r = urllib2.urlopen(req, context=_context)

    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError, e:
        resp = e.read()
        status_code = e.code
Beispiel #12
0
def image_tagging_aksk(_ak,
                       _sk,
                       image,
                       url,
                       languzge,
                       limit=-1,
                       threshold=0.0):
    endpoint = utils.get_endpoint(ais.AisService.IMAGE_SERVICE)
    _url = 'https://%s/v1.0/image/tagging' % endpoint

    sig = signer.Signer()
    sig.AppKey = _ak
    sig.AppSecret = _sk

    _data = {
        "image": image,
        "url": url,
        "language": languzge,
        "limit": limit,
        "threshold": threshold
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = endpoint
    kreq.uri = "/v1.0/image/tagging"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
        r = urllib2.urlopen(req, context=_context)

    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError, e:
        resp = e.read()
        status_code = e.code
Beispiel #13
0
 def get_zone_id(self, domain: str):
     url = 'https://dns.myhuaweicloud.com/v2/zones?type=public'
     r = signer.HttpRequest('GET', url)
     self.sign.Sign(r)
     res = json.loads(requests.get(url, headers=r.headers).text)
     # print(res)
     zone_id = ''
     for i in range(0, len(res['zones'])):
         if domain == res['zones'][i]['name'][:-1]:
             zone_id = res['zones'][i]['id']
     if zone_id != '':
         return zone_id
     else:
         return "The domain doesn't exist"
Beispiel #14
0
def _moderation_video_aksk(sig,
                           url,
                           frame_interval=5,
                           categories=['politics', 'terrorism']):
    _url = 'https://ais.cn-north-1.myhuaweicloud.com/v1.0/moderation/video'

    _data = {
        "url": url,
        "frame_interval": frame_interval,
        "categories": categories
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = "ais.cn-north-1.myhuaweicloud.com"
    kreq.uri = "/v1.0/moderation/video"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib.request.Request(url=_url,
                                     data=kreq.body,
                                     headers=kreq.headers)
        r = urllib.request.urlopen(req, context=_context)
    #
    # We use HTTPError and URLError,because urllib can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError as e:
        resp = e.read()
        status_code = e.code
    except URLError as e:
        resp = e.read()
        status_code = e.code
    else:
        status_code = r.code
        resp = r.read()
    return status_code, resp.decode('utf-8')
Beispiel #15
0
 def del_record(self, domain: str, record: str):
     zone_id = self.get_zone_id(domain)
     if zone_id != "The domain doesn't exist":
         url = 'https://dns.myhuaweicloud.com/v2/zones/' + zone_id + '/recordsets/' + record
     else:
         return "The domain doesn't exist"
     r = signer.HttpRequest('DELETE', url)
     self.sign.Sign(r)
     res = json.loads(requests.delete(url, headers=r.headers).text)
     # print(res['status'])
     try:
         if res['status'] == 'PENDING_DELETE':
             return 'success'
     except:
         return res
Beispiel #16
0
def moderation_image_aksk(_ak,
                          _sk,
                          image,
                          url,
                          categories=None,
                          threshold=None):
    _url = 'https://%s/v1.0/moderation/image' % ais.AisEndpoint.ENDPOINT

    sig = signer.Signer()
    sig.AppKey = _ak
    sig.AppSecret = _sk

    _data = {
        "image": image,
        "url": url,
        "categories": categories,
        "threshold": threshold,
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = ais.AisEndpoint.ENDPOINT
    kreq.uri = "/v1.0/moderation/image"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
        r = urllib2.urlopen(req, context=_context)
    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError, e:
        resp = e.read()
        status_code = e.code
Beispiel #17
0
    def get_record(self, domain, length, sub_domain, record_type):
        zone_id = self.get_zone_id(domain)
        if zone_id != "The domain doesn't exist":
            url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets?limit=' + str(
                length)
        else:
            return "The domain doesn't exist"
        r = signer.HttpRequest('GET', url)
        self.sign.Sign(r)

        resp = requests.get(url, headers=r.headers)
        try:
            resp.raise_for_status()
            res = json.loads(resp.text.decode('utf-8'))
        except:
            pass

        #替换为标准格式
        # {
        #   "data":{
        #     "records":[
        #       {
        #         "id":"record_id",
        #         "line":"线路",
        #         "value":"ip值"
        #       }
        #     ]
        #   }
        # }
        records = []
        try:
            for i in range(0, len(res['recordsets'])):
                #由于华为api的查询参数无法对subdomain和type进行过滤,所以只能在结果中进行判断
                if res['recordsets'][i]['name'].split(
                        '.')[0] == sub_domain and res['recordsets'][i][
                            'type'] == record_type and len(
                                res['recordsets'][i]['records']) > 0:
                    for ip in res['recordsets'][i]['records']:
                        records.append({
                            'id': res['recordsets'][i]['id'],
                            'line': res['recordsets'][i]['line'],
                            'value': ip
                        })
        except:
            pass

        return {"data": {"records": records}}
Beispiel #18
0
def request_moderation_url_aksk(sig, inner_path, image_str=None, url=None):
    _url = 'https://ais.cn-north-1.myhuaweicloud.com' + inner_path

    if image_str != '':
        image_str = image_str.decode('utf-8')

    _data = {"image": image_str, "url": url}

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = "ais.cn-north-1.myhuaweicloud.com"
    kreq.uri = inner_path
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib.request.Request(url=_url,
                                     data=kreq.body,
                                     headers=kreq.headers)
        r = urllib.request.urlopen(req, context=_context)

        #
        # We use HTTPError and URLError,because urllib can't process the 4XX &
        # 500 error in the single urlopen function.
        #
        # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
        # there is no this problem.
        #
    except HTTPError as e:
        resp = e.read()
        status_code = e.code
    except URLError as e:
        resp = e.read()
        status_code = e.code
    else:
        status_code = r.code
        resp = r.read()
    return resp.decode('utf-8')
Beispiel #19
0
    def del_record(self, domain, record):
        zone_id = self.get_zone_id(domain)
        if zone_id != "The domain doesn't exist":
            url = 'https://dns.myhuaweicloud.com/v2/zones/' + zone_id + '/recordsets/' + record
        else:
            return "The domain doesn't exist"
        r = signer.HttpRequest('DELETE', url)
        self.sign.Sign(r)

        #替换为标准格式
        # {
        #   "result":True,
        #   "message":{...}
        # }
        resp = requests.delete(url, headers=r.headers)
        try:
            resp.raise_for_status()
            res = json.loads(resp.text.decode('utf-8'))
        except HTTPError as httpError:
            response_status_code = httpError.response.status_code
            #response_header_params = httpError.response.headers
            #request_id = response_header_params["X-Request-Id"]
            #response_body = httpError.response.text
            return {
                "result": False,
                "message":
                "错误码:%s,错误描述:%s" % (response_status_code, response_body)
            }
        except:
            pass
        # print(res['status'])
        #try:
        #    if res['status'] == 'PENDING_DELETE':
        #        return 'success'
        #except:
        #    return res
        data = {}
        data["result"] = 'id' in ret
        data["message"] = res['status']
        if data["message"] == 'PENDING_DELETE':
            data["message"] = 'success'
        return data
Beispiel #20
0
 def change_record(self, domain: str, record_id: str, value: str, ttl: int):
     zone_id = self.get_zone_id(domain)
     if zone_id != "The domain doesn't exist":
         url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets/' + record_id
     else:
         return "The domain doesn't exist"
     data = {
         "records": [value],
         "ttl": ttl,
     }
     r = signer.HttpRequest('PUT', url, body=json.dumps(data))
     self.sign.Sign(r)
     res = json.loads(
         requests.put(url, headers=r.headers, data=r.body).text)
     # print(res)
     try:
         if res['status'] == 'PENDING_UPDATE' or res['status'] == 'ACTIVE':
             return 'success'
     except:
         return res
Beispiel #21
0
def asr_bgm_aksk(_ak, _sk, url):

    _url = 'https://ais.cn-north-1.myhuaweicloud.com/v1.0/bgm/recognition'

    sig = signer.Signer()
    sig.AppKey = _ak
    sig.AppSecret = _sk
    _data = {
        "url": url,
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = "ais.cn-north-1.myhuaweicloud.com"
    kreq.uri = "/v1.0/bgm/recognition"
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
        resp = urllib2.urlopen(req, context=_context)
    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError, e:
        resp = e.read()
        status_code = e.code
Beispiel #22
0
    def change_record(self, domain: str, record_id: str, value: str, ttl: int):
        zone_id = self.get_zone_id(domain)
        if zone_id != "The domain doesn't exist":
            url = 'https://dns.myhuaweicloud.com/v2.1/zones/' + zone_id + '/recordsets/' + record_id
        else:
            return "The domain doesn't exist"
        data = {
            "records": [value],
            "ttl": ttl,
        }
        r = signer.HttpRequest('PUT', url, body=json.dumps(data))
        self.sign.Sign(r)
        #替换为标准格式
        # {
        #   "result":True,
        #   "message":{...}
        # }
        resp = requests.put(url, headers=r.headers, data=r.body)
        try:
            resp.raise_for_status()
            res = json.loads(resp.text.decode('utf-8'))
        except HTTPError as httpError:
            response_status_code = httpError.response.status_code
            #response_header_params = httpError.response.headers
            #request_id = response_header_params["X-Request-Id"]
            #response_body = httpError.response.text
            return {
                "result": False,
                "message":
                "错误码:%s,错误描述:%s" % (response_status_code, response_body)
            }
        except:
            pass

        data = {}
        data["result"] = 'id' in ret
        data["message"] = res['status']
        if data["message"] == 'PENDING_DELETE':
            data["message"] = 'success'
        return data
Beispiel #23
0
def request_moderation_url_aksk(sig, inner_path, image_str=None, url=None):
    _url = 'https://' + ais.AisEndpoint.MODERATION_ENDPOINT + inner_path

    _data = {
        "image": image_str,
        "url": url
    }

    kreq = signer.HttpRequest()
    kreq.scheme = "https"
    kreq.host = ais.AisEndpoint.MODERATION_ENDPOINT
    kreq.uri = inner_path
    kreq.method = "POST"
    kreq.headers = {"Content-Type": "application/json"}
    kreq.body = json.dumps(_data)

    resp = None
    status_code = None
    try:
        sig.Sign(kreq)
        #
        # Here we use the unvertified-ssl-context, Because in FunctionStage
        # the client CA-validation have some problem, so we must do this.
        #
        _context = ssl._create_unverified_context()
        req = urllib2.Request(url=_url, data=kreq.body, headers=kreq.headers)
        r = urllib2.urlopen(req, context=_context)

    #
    # We use HTTPError and URLError,because urllib2 can't process the 4XX &
    # 500 error in the single urlopen function.
    #
    # If you use a modern, high-level designed HTTP client lib, Yeah, I mean requests,
    # there is no this problem.
    #
    except HTTPError, e:
        resp = e.read()