forked from fruitlollipop/OKEXAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpsMD5Util.py
More file actions
69 lines (61 loc) · 2.28 KB
/
HttpsMD5Util.py
File metadata and controls
69 lines (61 loc) · 2.28 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
64
65
66
67
68
69
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 用于进行https请求,以及MD5加密,生成签名的工具类
import http.client
import urllib.parse
import json
import hashlib
class HttpsRequest(object):
def __init__(self, url):
"""
Constructor for class of HttpsRequest.
:param url: Base URL for the Request methods
:return: None
"""
self.__url = url
@classmethod
def build_sign(cls, params, secret_key):
"""
To build MD5 sign for user's parameters.
:param params: User's parameters usually in the format of a dict
:param secret_key: String of SECRET KEY
:return: Signed string encrypted by MD5
"""
sign = ''
if hasattr(params, 'items'):
for key in sorted(params.keys()):
sign += key + '=' + str(params[key]) + '&'
data = sign + 'secret_key=' + secret_key
else:
raise TypeError('{0} should has attributes of "items"'.format(params))
return hashlib.md5(data.encode('utf8')).hexdigest().upper()
def get(self, resource, params=''):
"""
GET method to request resources.
:param resource: String of URL for resources
:param params: String of user's parameters without encryption
:return: JSON of the response of the GET request
"""
conn = http.client.HTTPSConnection(self.__url, timeout=10)
conn.request('GET', resource + '?' + params)
response = conn.getresponse()
data = response.read().decode('utf-8')
conn.close()
return json.loads(data)
def post(self, resource, params_dict):
"""
POST method to request resources.
:param resource: String of URL for resources
:param params_dict: User's parameters to be encrypted, usually in the format of a dict
:return: Response of the GET request
"""
headers = {
'Content-type': 'application/x-www-form-urlencoded',
}
conn = http.client.HTTPSConnection(self.__url, timeout=10)
body = urllib.parse.urlencode(params_dict)
conn.request("POST", resource, body, headers)
response = conn.getresponse()
data = response.read().decode('utf-8')
conn.close()
return data