Python2
Python 2.X
#!/usr/bin/env python2
#encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import json
import time
import hashlib
import requests
import random
import string
TOKEN = "5c9dc98fb92e2"
SECRET = "hkyavn13ybc1qunp4rpb"
HOST = "http://api.xxx.com"
'''
request用法说明
args = dict()
args['url'] = 'http://blog.csdn.net/iloveyin/article/details/21444613'
args['method'] = 'GET'
args['cookies'] = {}
args['data'] = {}
args['header'] = {"user-agent":"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)"}
args['proxy'] = '10.0.0.5'
args['timeout'] = 30
args['encoding'] = 'gbk'
result = request(args)
result['code']
result['content']
'''
#http请求方法
def request(args):
if not (args.has_key('timeout')):
args['timeout'] = 60
#代理设置proxy
if not (args.has_key('proxy')):
proxies = None
else:
if not args.has_key('proxy') == '':
if not args.has_key('proxy_type'):
args['proxy_type'] = 'http'
if args['proxy_type'] == 'http':
proxies = {"http":"http://"+args['proxy'],"https":"http://"+args['proxy']}
elif args['proxy_type'] == 'socks':
proxies = {"http":"socks5://"+args['proxy'],"https":"socks5://"+args['proxy']}
else:
proxies = None
if not (args.has_key('method')):
args['method'] = 'GET'
else:
args['method'] = args['method'].upper()
#header设置
if not (args.has_key('headers')):
args['headers'] = {}
if not args['headers'].has_key('user-agent'):
args['headers']['user-agent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
#Cookies
cookies = {}
if args.has_key('cookies'):
cookies = args['cookies']
if not args.has_key('data'):
args['data'] = {}
args['headers'].update(mkHeader(args['data']))
if args['method'] == 'GET':
r = requests.get(args['url'], params=args['data'],cookies=cookies,headers=args['headers'],timeout=int(args['timeout']), proxies=proxies, verify=False)
elif args['method'] == 'POST':
r = requests.post(args['url'], data=args['data'],cookies=cookies,headers=args['headers'],timeout=int(args['timeout']), proxies=proxies, verify=False)
result = {}
result['code'] = r.status_code
ck = {}
for cookie in r.cookies:
ck.update({cookie.name : cookie.value})
result['cookies'] = ck
result['headers'] = r.headers
result['content'] = r.content
return result
#http 带签名的header生成方法
def mkHeader(data=dict()):
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 5))
Nonce = "%d_%s" % (int(time.time()),ran_str)
header=dict()
header['Token'] = TOKEN
header['Nonce'] = Nonce
header['Signature'] = sign(Nonce,data)
return header
#签名生成方法
def sign(Nonce,data=dict()):
tmp = list()
tmp.append(TOKEN)
tmp.append(SECRET)
tmp.append(Nonce)
for d,x in data.items():
tmp.append(str(d) + "=" + str(x))
return hashlib.sha1(''.join(sorted(tmp))).hexdigest()
if __name__ == '__main__':
#获取K线
args = dict()
args['url'] = HOST + '/openApi/market/kline'
args['method'] = 'GET'
args['data'] = dict()
args['data']['symbol'] = "BTC-USDT"
args['data']['period'] = "1min"
args['data']['size'] = 2000
result = request(args)
if result['code']==200:
print result['content']
else:
print result
print '-'*100
#查询我的资产
args = dict()
args['url'] = HOST + '/openApi/wallet/list'
args['method'] = 'GET'
args['data'] = dict()
args['data']['currency'] = "BTC"
result = request(args)
if result['code']==200:
print result['content']
else:
print result
print '-'*100
#委托挂单
args = dict()
args['url'] = HOST + '/openApi/entrust/add'
args['method'] = 'POST'
args['data'] = dict()
args['data']['symbol'] = "BTC-USDT"
args['data']['type'] = "buy-limit"
args['data']['amount'] = 1.343432
args['data']['price'] = 500.323214
result = request(args)
if result['code']==200:
print result['content']
else:
print result