Python3

Python
#!/usr/bin/env python
#encoding=utf8

import sys
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 'timeout' in args:
        args['timeout'] = 60


    if not 'method' in args:
        args['method'] = 'GET'
    else:
        args['method'] = args['method'].upper()

#header設置
    if not 'headers' in args:
        args['headers'] = {}

    if not 'user-agent' in args['headers']:
        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 'cookies' in args:
        cookies = args['cookies']

    if not 'data' in args:
        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']),  verify=False)
    elif args['method'] == 'POST':
        r = requests.post(args['url'], data=args['data'],cookies=cookies,headers=args['headers'],timeout=int(args['timeout']), 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)).encode("utf8")).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'].decode("utf8"))
    else:
        print(result)