将PHP翻译成Python(Rest-API连接)

Tig*_*oop 5 php python python-2.7 bitcoin

我正在学习Python,作为练习,我尝试制作一个程序来在比特币市场上进行交易:https://bitcurex.com.以下是API参考:https://bitcurex.com/reading-room/API.有一个PHP客户端示例,所以我尝试将其转换为Python,所以我得到:

import math
import time
import simplejson
import urllib
import urllib2
import hmac,hashlib

def microtime():
    return '%f %d' % math.modf(time.time())

def query( path, key, secret, data={} ):
    mt = microtime().split()
    nonce = mt[1] + mt[0][2:]
    data['nonce'] = nonce

    post_data = urllib.urlencode( data )

    sign = hmac.new( secret.decode('base64'), post_data, hashlib.sha512 ).digest()

    headers = {'Rest-Key' : key,
               'Rest-Sign': sign.encode('base64').strip(),
               'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
               'Content-type': 'application/x-www-form-urlencoded'}
    print headers

    url = 'https://bitcurex.com/api/0/' + path

    req = urllib2.Request( url, post_data, headers )
    response = urllib2.urlopen(req)

    return simplejson.loads(response.read())

print query('getFunds', '29a28e8fe234537056a8b256c0df50413f50da9c49ca61991ea8b8f108a88e09',  'y2NDxKGa/xvhtXrDP+3oscbBUFSac9+T8jzu2nRmt0vBdHbbl8NRqdmxKFr2IwwY5LAskTQZGyy2XONaNN6Jrg==')
Run Code Online (Sandbox Code Playgroud)

这些API密钥正常工作 - 您只能使用它们进行getFunds查询.

它一直返回错误"我必须登录".我试图通过Fiddler代理调试器查看该请求,在这里你有这个尝试的标题:

POST /api/0/getFunds HTTP/1.1
Accept-Encoding: identity
Rest-Sign: Dd1WBn2T5SYTbqMMohOxr46IaLDrkelgH7AgkrrB0mT0PxKfv15vSJ3b6xNdc5PO2Yz9cDpu0u/H
WIc7bH56sQ==: 
Content-Length: 22
Rest-Key: 29a28e8fe234537056a8b256c0df50413f50da9c49ca61991ea8b8f108a88e09
Connection: close
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)
Host: bitcurex.com
Content-Type: application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)

Fiddler向我显示错误:

Incorrectly formed request headers.
Missing colon in header #3, WIc7bH56sQ==
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?看起来我的Rest-Sign太长了或类似的东西.我认为我的代码应该与PHP示例完全相同.我做错了什么?