-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathHttpUtil.py
44 lines (36 loc) · 1.21 KB
/
HttpUtil.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
42
43
44
#!/usr/bin/python
# -*- coding: utf-8 -*-
import http.client
import urllib
import json
import hashlib
import hmac
def getSign(params, secretKey):
bSecretKey = bytes(secretKey, encoding='utf8')
sign = ''
for key in params.keys():
value = str(params[key])
sign += key + '=' + value + '&'
bSign = bytes(sign[:-1], encoding='utf8')
mySign = hmac.new(bSecretKey, bSign, hashlib.sha512).hexdigest()
return mySign
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')
return json.loads(data)
def httpPost(url, resource, params, apiKey, secretKey):
headers = {
"Content-type" : "application/x-www-form-urlencoded",
"KEY":apiKey,
"SIGN":getSign(params, secretKey)
}
conn = http.client.HTTPSConnection(url, timeout=10)
tempParams = urllib.parse.urlencode(params) if params else ''
conn.request("POST", resource, tempParams, headers)
response = conn.getresponse()
data = response.read().decode('utf-8')
params.clear()
conn.close()
return data