python阿里云api查询域名是否可以注册(CheckDomain)

 

import requests
from fun import *
from urllib import parse

url ='http://domain.aliyuncs.com/?'
access_key_id = '${AccessKeyId}'
access_key_secret = '${AccessKeySecret}'

params = {
    'Action': 'CheckDomain',
    'DomainName': 'huangzhengxin.com',
    'Format': 'JSON',
    'Version': '2018-01-29',
    'SignatureMethod': 'HMAC-SHA1',
    'SignatureVersion': '1.0',
    'AccessKeyId': access_key_id,
}
params.update(SignatureNonce=getRandom())
params.update(Timestamp=getTimestamp())

stringToSign = getStringToSign(params)

signature = hmac_sha1(access_key_secret + '&', stringToSign)

params['Signature'] = signature

url = url + parse.urlencode(params)

r = requests.get(url)

print("status code", r.status_code)
# print(r.text)
print(r.json())

 

fun.py

from urllib.parse import quote
from hashlib import sha1
import hmac
import base64
import datetime
import time

def hmac_sha1(key, s):
    h = hmac.new(key.encode('utf-8'), s.encode("utf8"), sha1)
    signature = base64.b64encode(h.digest())
    return signature

def percentEncode(string):
    res = quote(string, encoding='utf-8')
    res = res.replace('+', '%20')
    res = res.replace('*', '%2A')
    res = res.replace('%7E', '~')
    return res

def getTimestamp():
    return datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")

def getRandom():
    return str(time.time()).replace('.', '')

def getStringToSign(params):
    sortedD = sorted(params.items(), key=lambda x: x[0])
    canstring = ''
    for k, v in sortedD:
        canstring += '&' + percentEncode(k) + '=' + percentEncode(v)

    stringToSign = 'GET&%2F&' + percentEncode(canstring[1:])
    return stringToSign

 

你可能感兴趣的:(python阿里云api查询域名是否可以注册(CheckDomain))