将PHP转换为Python(RestAPI连接)

2024-10-03 06:24:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习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==')

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

它一直返回错误“必须我登录”。我试图通过Fiddler Proxy Debugger查看该请求,下面是该尝试的标题:

^{pr2}$

小提琴手给我看了一个错误:

Incorrectly formed request headers.
Missing colon in header #3, WIc7bH56sQ==

有什么想法吗?我的休息标志好像太长了什么的。我认为我的代码应该与PHP示例完全相同。我做错什么了?在


Tags: httpsimportcomapi示例datatimemath
3条回答

从磁盘删除项目后,Eclipse无法恢复它。但是,根据您的操作系统,它可能仍在您的垃圾箱文件夹中。如果不是,那么这确实是一个数据恢复的问题

Eclipse - Managing Files and Projects说:

To delete a project right click on the project in the package viewer. Again, select "Delete" from the context menu. This time, a dialog box will appear asking you if you want to also delete all of the files in the project's directory. The default is "Do not delete contents". If you select this option, the Java files that were a part of that project will not be deleted. If you are sure you no longer need the project delete the contents instead.

所以,您可能刚刚从eclipse中删除了它,但它仍然在您的硬盘上。否则,您必须使用Data Recovery Softwares

如果您只是碰巧在eclipse中删除了该项目,但未选中“删除磁盘上的项目内容(无法撤消)”,则可以恢复该项目。这是通过使用文件/导入、选择“将现有项目导入工作区”并浏览硬盘上的位置来完成的,在那里您已经精确地存储了项目

相关问题 更多 >