12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- # -*- coding: utf-8 -*-
- import os
- import sys
- import json
- import time
- import datetime
- import random
- import urllib
- import urllib2
- import ssl
- import gzip
- import StringIO
- import binascii
- import hmac
- import hashlib
- from traceback import format_exc
- from copy import deepcopy
- from collections import OrderedDict
- class QcloudAPI(object):
- def __init__(self, secretid="AKIDOh7ZouTY4OWtDZp9zrrfGsyxDLL9mkdy",
- secretkey="RhlcaaJuKYzRPGvO3e4MTPBlS6Zq197G"):
- self.secretid = secretid
- self.secretkey = secretkey
- self.params = {}
- def gen_sign(self, url, params, method='GET'):
- srcstr = method.upper() + url + '?' + "&".join(k.replace("_",".") + "=" + str(params[k]) for k in sorted(params.keys()))
- # print srcstr
- hashed = hmac.new(self.secretkey, srcstr, hashlib.sha1)
- return binascii.b2a_base64(hashed.digest())[:-1]
- def refresh_cnd_dir(self, url):
- req_url = "cdn.api.qcloud.com/v2/index.php"
- params = OrderedDict(deepcopy(self.params))
- params["Action"] = "RefreshCdnDir"
- params['SecretId'] = self.secretid
- params["Timestamp"] = int(time.time())
- params["Nonce"] = random.randint(1, 1000000000)
- params["dirs.0"] = url
- print params
- params["Signature"] = self.gen_sign(req_url, params)
- _req_url = "?".join(["https://"+req_url, urllib.urlencode(params)])
- resp = send_req(_req_url)
- print resp
- return resp
|