首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用python创建websocket包装器

如何使用python创建websocket包装器
EN

Stack Overflow用户
提问于 2017-11-24 23:34:36
回答 0查看 439关注 0票数 1

我目前正在开发一个项目,为gemini api (https://docs.gemini.com/rest-api/)创建一个python包装器。我正在处理订单事件websocket (https://docs.gemini.com/websocket-api/#order-events),但在使用python的websocket客户端时遇到了困难。这就是我现在的代码:

代码语言:javascript
运行
复制
import json
import hmac
import hashlib
import base64
import time
from websocket import create_connection
import requests

public_key = 'asdfkjdfdfk'
private_key = 'asdfkjdfdfk'
url = "https://api.sandbox.gemini.com/v1/order/status"


def header(method, payload=None):
    if payload is None:
        payload = {}
    payload['request'] = method
    payload['nonce'] = int(time.time() * 1000)
    payload['order_id'] = '86499545'
    b64_payload = base64.b64encode(json.dumps(payload).encode('utf-8'))
    signature = hmac.new(private_key.encode('utf-8'), b64_payload, hashlib.sha384).hexdigest()

    headers = {
        'X-GEMINI-APIKEY': public_key,
        'X-GEMINI-PAYLOAD': b64_payload,
        'X-GEMINI-SIGNATURE': signature,
    }
    return headers


r = requests.post(url, headers=header('/v1/order/status'))
print(r.json())

ws = create_connection("wss://api.sandbox.gemini.com/v1/order/events",
                       header=header('/v1/order/events'))

当我运行脚本时,我得到以下错误:

代码语言:javascript
运行
复制
{'order_id': '86499545', 'id': '86499545', 'symbol': 'btcusd', 'exchange': 'gemini', 'avg_execution_price': '0.00', 'side': 'buy', 'type': 'exchange limit', 'timestamp': '1511299791', 'timestampms': 1511299791519, 'is_live': False, 'is_cancelled': True, 'is_hidden': False, 'was_forced': False, 'executed_amount': '0', 'remaining_amount': '0.1', 'options': ['maker-or-cancel'], 'price': '7900.00', 'original_amount': '0.1'}
Traceback (most recent call last):
  File "C:\Users\uzman\Documents\Python\Gemini- Python API Wrapper\gemini\testing.py", line 35, in <module>
    header=header('/v1/status/order'))
  File "C:\Users\uzman\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_core.py", line 487, in create_connection
    websock.connect(url, **options)
  File "C:\Users\uzman\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_core.py", line 214, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "C:\Users\uzman\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_handshake.py", line 63, in handshake
    headers, key = _get_handshake_headers(resource, hostname, port, options)
  File "C:\Users\uzman\AppData\Local\Programs\Python\Python36\lib\site-packages\websocket\_handshake.py", line 110, in _get_handshake_headers
    headers.extend(header)
TypeError: sequence item 1: expected str instance, bytes found
[Finished in 1.5s]

正如您从第一个请求中看到的,api成功地允许我们创建一个订单,这意味着验证工作正常。然而,问题似乎出在websocket模块上。我似乎不知道如何成功地创建web套接字连接。我知道我使用utf8编码对b64_payloadsignature进行了编码,但这正是gemini api要求您做的,我不能更改它,因为如果我这样做了,那么第一个请求就不能工作

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47476347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档