utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import json
  5. import time
  6. import datetime
  7. import random
  8. import urllib
  9. import urllib2
  10. import ssl
  11. import gzip
  12. import StringIO
  13. import binascii
  14. import hmac
  15. import hashlib
  16. from traceback import format_exc
  17. from copy import deepcopy
  18. from collections import OrderedDict
  19. class QcloudAPI(object):
  20. def __init__(self, secretid="AKIDOh7ZouTY4OWtDZp9zrrfGsyxDLL9mkdy",
  21. secretkey="RhlcaaJuKYzRPGvO3e4MTPBlS6Zq197G"):
  22. self.secretid = secretid
  23. self.secretkey = secretkey
  24. self.params = {}
  25. def gen_sign(self, url, params, method='GET'):
  26. srcstr = method.upper() + url + '?' + "&".join(k.replace("_",".") + "=" + str(params[k]) for k in sorted(params.keys()))
  27. # print srcstr
  28. hashed = hmac.new(self.secretkey, srcstr, hashlib.sha1)
  29. return binascii.b2a_base64(hashed.digest())[:-1]
  30. def refresh_cnd_dir(self, url):
  31. req_url = "cdn.api.qcloud.com/v2/index.php"
  32. params = OrderedDict(deepcopy(self.params))
  33. params["Action"] = "RefreshCdnDir"
  34. params['SecretId'] = self.secretid
  35. params["Timestamp"] = int(time.time())
  36. params["Nonce"] = random.randint(1, 1000000000)
  37. params["dirs.0"] = url
  38. print params
  39. params["Signature"] = self.gen_sign(req_url, params)
  40. _req_url = "?".join(["https://"+req_url, urllib.urlencode(params)])
  41. resp = send_req(_req_url)
  42. print resp
  43. return resp