forked from suixinio/okexAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HttpMD5Util.py
41 lines (33 loc) · 1.11 KB
/
HttpMD5Util.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#用于进行http请求,以及MD5加密,生成签名的工具类
import http.client
import urllib
import json
import hashlib
import time
def buildMySign(params,secretKey):
sign = ''
for key in sorted(params.keys()):
sign += key + '=' + str(params[key]) +'&'
data = sign+'secret_key='+secretKey
return hashlib.md5(data.encode("utf8")).hexdigest().upper()
def httpGet(url,resource,params=''):
conn = http.client.HTTPSConnection(url, timeout=10)
conn.request("GET",resource + '?' + params)
response = conn.getresponse()
data = response.read().decode('utf-8')
conn.close()
return json.loads(data)
def httpPost(url,resource,params):
headers = {
"Content-type" : "application/x-www-form-urlencoded",
}
conn = http.client.HTTPSConnection(url, timeout=10)
temp_params = urllib.parse.urlencode(params)
conn.request("POST", resource, temp_params, headers)
response = conn.getresponse()
data = response.read().decode('utf-8')
params.clear()
conn.close()
return data