[英]How to pass token to get connected with Websocket API using python Websocket-client lib
我正在嘗試使用Websocket API從Indodax交換訪問訂單簿數據。 文檔可以在這里找到。 . 我正在使用帶有websocket-client庫的 python 3.9。
根據文檔配置所有內容后,它給了我不好的請求。 我已經按照this answer中給出的示例在握手期間啟用了令牌。
--- request header ---
GET /ws/ HTTP/1.1
Upgrade: websocket
Host: ws.indodax.com
Origin: http://ws.indodax.com
Sec-WebSocket-Key: X+jRgd8pM0XefcT1/xwHNQ==
Sec-WebSocket-Version: 13
Connection: Upgrade
extension-token:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaW5mbyI6eyJuYW1lIjoiUHVibGljIn19.VJAHTrrfwxceSITpuPBl75LVM5bgojKGiUTOwCZxw-k
-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Server: nginx
Date: Wed, 20 Oct 2021 10:41:13 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: rI37u4yeNItEuZP5MeqZ254XGuo=
Via: 1.1 google
Alt-Svc: clear
-----------------------
++Sent raw: b'\x81\x8eb\xb2\x18\xc9@\xd0l\xaa\x0b\xd6j\xe7\x16\xc0y\xad\x07\x90'
++Sent decoded: fin=1 opcode=1 data=b'"btcidr.trade"'
thread terminating...
++Rcv raw: b'\x88,\x0b\xbb{"reason":"bad request","reconnect":false}'
++Rcv decoded: fin=1 opcode=8 data=b'\x0b\xbb{"reason":"bad request","reconnect":false}'
++Sent raw: b'\x88\x82\xc3Q K\xc0\xb9'
++Sent decoded: fin=1 opcode=8 data=b'\x03\xe8'
### closed ###
import websocket
import json
import _thread
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print("### closed ###")
def on_open(ws):
subscribeEvent(ws, "btcidr"+".trade")
print("thread terminating...")
_thread.start_new_thread(send_heartbeat, ())x`
def send_heartbeat(*args):
pass
def subscribeEvent(ws, event, auth_key=''):
try:
ws.send(json.dumps(event))
except Exception as e:
print(e)
if __name__ == "__main__":
websocket.enableTrace(True)
token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaW5mbyI6eyJuYW1lIjoiUHVibGljIn19.VJAHTrrfwxceSITpuPBl75LVM5bgojKGiUTOwCZxw-k'
protocol_str = "extension-token:" + token
while True:
ws = websocket.WebSocketApp("wss://ws.indodax.com/ws/",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
header = [protocol_str])
ws.run_forever()
我認為問題在於令牌。 我的意思是關於我應該如何將令牌傳遞給服務器的一些格式問題。 如果您需要任何其他信息,請告訴我。
在 URL 中添加您的令牌作為可選參數。 此片段中的 WS_HOST 可能是您的 wss://ws.indodax.com/ws/
URL = '{}?token={}'.format(WS_HOST, TOKEN)
ws = websocket.WebSocketApp(URL,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close,
header = [protocol_str])
ws.run_forever()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.