网宿 CDN 刷新
chenzuoqing Lv3

网宿 CDN 刷新

适用网宿 CDN 刷新,封装了签名,执行 sendRequest 调用服务商接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class WangsuApi(object):
"""
:func: 网速CDN访问API刷新缓存的工具类
:create on: 2019-04-08
"""

def __init__(self, userName, apiKey):
self.date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
self.apiAddress = "https://open.chinanetcenter.com/"
self.authStr = self.getAuth(userName, apiKey)

def getAuth(self, userName, apikey):
signed_apikey = hmac.new(apikey.encode('utf-8'), self.date.encode('utf-8'), hashlib.sha256).digest()
signed_apikey = base64.b64encode(signed_apikey)
signed_apikey = userName + ":" + signed_apikey.decode()
signed_apikey = base64.b64encode(signed_apikey.encode('utf-8'))
return signed_apikey

def createHeader(self):
headers = {
'Date': self.date,
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': 'Basic ' + self.authStr.decode()
}
return headers

def sendRequest(self, apiUrl, httpBodyParams, method="POST"):
httpUrl = self.apiAddress + apiUrl
headers = self.createHeader()
if method.upper() == 'POST':
resp = requests.post(httpUrl, data=json.dumps(httpBodyParams), headers=headers)
elif method.upper() == 'GET':
resp = requests.get(httpUrl, headers=headers)
else:
return None
# self.printResp(resp)
return resp.json()

@staticmethod
def printResp(resp):
headers_post = dict(resp.headers)
tmp_str = "statusCode:{}\nDate:{}\nContent-Length:{}\nConnection:{}\nx-cnc-request-id:{}\n\n{}".format(
resp.status_code,
headers_post.get('Date'),
headers_post.get('Content-Length'),
headers_post.get('Connection'),
headers_post.get('x-cnc-request-id'),
resp.text)
print(tmp_str)
 Comments