-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHuobiDMUtil.py
More file actions
110 lines (92 loc) · 3.79 KB
/
HuobiDMUtil.py
File metadata and controls
110 lines (92 loc) · 3.79 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date : 20180917
# @Author : zhaobo
# @github :
import base64
import hmac
import hashlib
import json
import urllib
import datetime
import requests
#import urlparse # urllib.parse in python 3
# timeout in 5 seconds:
TIMEOUT = 10
#my_proxies={"http":"http://127.0.0.1:1080","https":"https://127.0.0.1:1080"}
my_proxies={"http":None,"https":None}
#各种请求,获取数据方式
def http_get_request(url, params, add_to_headers=None):
headers = {
"Content-type": "application/x-www-form-urlencoded",
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0'
}
if add_to_headers:
headers.update(add_to_headers)
postdata = urllib.parse.urlencode(params)
try:
response = requests.get(url, postdata, proxies=my_proxies, headers=headers, timeout=TIMEOUT)
if response.status_code == 200:
return response.json()
else:
return {"status":"fail"}
except Exception as e:
print("httpGet failed, detail is:%s" %e)
return {"status":"fail","msg": "%s"%e}
def http_post_request(url, params, add_to_headers=None):
headers = {
"Accept": "application/json",
'Content-Type': 'application/json',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0'
}
if add_to_headers:
headers.update(add_to_headers)
postdata = json.dumps(params)
try:
response = requests.post(url, postdata,proxies=my_proxies, headers=headers, timeout=TIMEOUT)
if response.status_code == 200:
return response.json()
else:
return response.json()
except Exception as e:
print("httpPost failed, detail is:%s" % e)
return {"status":"fail","msg": "%s"%e}
def api_key_get(url, request_path, params, ACCESS_KEY, SECRET_KEY):
method = 'GET'
timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
params.update({'AccessKeyId': ACCESS_KEY,
'SignatureMethod': 'HmacSHA256',
'SignatureVersion': '2',
'Timestamp': timestamp})
host_name = host_url = url
#host_name = urlparse.urlparse(host_url).hostname
host_name = urllib.parse.urlparse(host_url).hostname
host_name = host_name.lower()
params['Signature'] = createSign(params, method, host_name, request_path, SECRET_KEY)
url = host_url + request_path
return http_get_request(url, params)
def api_key_post(url, request_path, params, ACCESS_KEY, SECRET_KEY):
method = 'POST'
timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')
params_to_sign = {'AccessKeyId': ACCESS_KEY,
'SignatureMethod': 'HmacSHA256',
'SignatureVersion': '2',
'Timestamp': timestamp}
host_url = url
#host_name = urlparse.urlparse(host_url).hostname
host_name = urllib.parse.urlparse(host_url).hostname
host_name = host_name.lower()
params_to_sign['Signature'] = createSign(params_to_sign, method, host_name, request_path, SECRET_KEY)
url = host_url + request_path + '?' + urllib.parse.urlencode(params_to_sign)
return http_post_request(url, params)
def createSign(pParams, method, host_url, request_path, secret_key):
sorted_params = sorted(pParams.items(), key=lambda d: d[0], reverse=False)
encode_params = urllib.parse.urlencode(sorted_params)
payload = [method, host_url, request_path, encode_params]
payload = '\n'.join(payload)
payload = payload.encode(encoding='UTF8')
secret_key = secret_key.encode(encoding='UTF8')
digest = hmac.new(secret_key, payload, digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest)
signature = signature.decode()
return signature