# encoding: utf-8 import os import time import base64 import random import urllib, urllib2 import hmac, hashlib class AL_Flush(): alurl = "https://dcdn.aliyuncs.com/?" url = "download.gamenew-h5.com" AccessKeyId = "LTAIE4jRXhLBvVLL" AccessKeySecret = "74hEmfGzNEYLSFP26fO6CUZ319AY61" def percentEncode(self, value): #value = urllib.quote(value) #value = value.replace("%26", "&") value = value.replace("/", "%252F") value = value.replace("&", "%26") value = value.replace("=", "%3D") value = value.replace(":", "%253A") return value def flush(self, dic): t = time.time()-8*60*60 localtime = time.localtime(t) date = time.strftime("%Y-%m-%dT%H:%M:%SZ", localtime) keydict = { 'Action': 'RefreshDcdnObjectCaches', 'ObjectPath': self.url + '/' + dic, 'ObjectType': "Directory", 'Version': '2018-01-15', 'Timestamp': date, 'SignatureNonce': str(int(random.random() * 100000000)), 'AccessKeyId': self.AccessKeyId, 'SignatureMethod': 'HMAC-SHA1', 'SignatureVersion': '1.0' } keydict = sorted(keydict.iteritems(), key = lambda asd:asd[0] ,reverse = False) print keydict params = "" for k in keydict: if params != "": params += "&" params += k[0]+"="+k[1] print params StringToSign = "GET&%2F&" + self.percentEncode(params) #StringToSign = self.percentEncode(StringToSign) print StringToSign h = hmac.new(bytes(self.AccessKeySecret+"&"), bytes(StringToSign), hashlib.sha1).hexdigest() #h = hmac.new(b'hash',b'helloworld',digestmod='sha1') print(h) sign = base64.b64encode(h) print sign sign = self.percentEncode(sign) print sign u = self.alurl + params + "&Signature="+sign print u #response = urllib2.urlopen(u) #print response.read() a = b"testsecret&" b = b"GET&%2F&AccessKeyId%3Dtestid&Action%3DDescribeLiveSnapshotConfig&AppName%3Dtest&DomainName%3Dtest.com&Format%3DXML&RegionId%3Dcn-shanghai&ServiceCode%3Dlive&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3Dc2fe8fbb-2977-4414-8d39-348d02419c1c&SignatureVersion%3D1.0&Timestamp%3D2017-06-14T09%253A51%253A14Z&Version%3D2016-11-01" h = hmac.new(a, b, hashlib.sha1).hexdigest() #h = hmac.new(b'hash',b'helloworld',digestmod='sha1') print(h) sign = base64.b64encode(h) print sign if __name__ == '__main__': alf = AL_Flush() alf.flush("Japan/SW/")