-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk.py
More file actions
executable file
·63 lines (49 loc) · 1.68 KB
/
sdk.py
File metadata and controls
executable file
·63 lines (49 loc) · 1.68 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
import hashlib, json, httplib
import urlparse
import urllib
import sys
from config import *
class UCLOUDException(Exception):
def __str__(self):
return "Error"
def _verfy_ac(private_key, params):
items = params.items()
items.sort()
params_data = ""
for key, value in items:
params_data = params_data + str(key) + str(value)
params_data = params_data+private_key
'''use sha1 to encode keys'''
hash_new = hashlib.sha1()
hash_new.update(params_data)
hash_value = hash_new.hexdigest()
return hash_value
class UConnection(object):
def __init__(self, base_url):
self.base_url = base_url
o = urlparse.urlsplit(base_url)
if o.scheme == 'https':
self.conn = httplib.HTTPSConnection(o.netloc)
else:
self.conn = httplib.HTTPConnection(o.netloc)
def __del__(self):
self.conn.close()
def get(self, resouse, params):
resouse += "?" + urllib.urlencode(params)
print("%s%s" % (self.base_url, resouse))
self.conn.request("GET", resouse)
response = json.loads(self.conn.getresponse().read())
return response
class UcloudApiClient(object):
# 添加 设置 数据中心和 zone 参数
def __init__(self, base_url, public_key, private_key):
self.g_params = {}
self.g_params['PublicKey'] = public_key
self.private_key = private_key
self.conn = UConnection(base_url)
def get(self, uri, params):
# print params
_params = dict(self.g_params, **params)
_params["Signature"] = _verfy_ac(self.private_key, _params)
return self.conn.get(uri, _params)