簡體   English   中英

幣安 python websocket - 無響應

[英]Binance python websocket - no response

我正在嘗試使用python-binance並按照本教程運行binance websocket api的示例代碼: https://livedataframe.com/live-crypt5Z

不幸的是我不能讓它工作。 websocket 只是沒有響應導致空終端沒有打印任何內容的結果。

我正在使用 python 3.7.8,PyCharm IDE,ZAEA23489CE3AA943406EBB28E0

請幫忙,謝謝!

import time
from binance.client import Client # Import the Binance Client
from binance.websockets import BinanceSocketManager # Import the Binance Socket Manager

_API_KEY = "mykey"
_API_SECRET = "mykey"

client = Client(_API_KEY, _API_SECRET)

# Instantiate a BinanceSocketManager, passing in the client that you instantiated
bm = BinanceSocketManager(client)

# This is our callback function. For now, it just prints messages as they come.
def handle_message(msg):
    print(msg)

# Start trade socket with 'ETHBTC' and use handle_message to.. handle the message.
conn_key = bm.start_trade_socket('ETHBTC', handle_message)
# then start the socket manager
bm.start()

# let some data flow..
time.sleep(10)

# stop the socket manager
bm.stop_socket(conn_key)

此外,我可以運行此代碼,但 websocket api 似乎對我不起作用。 問候。

from binance.client import Client


_API_KEY = "key"
_API_SECRET = "key"

client = Client(_API_KEY, _API_SECRET)
btc_price = client.get_symbol_ticker(symbol="BTCUSDT")
# print full output (dictionary)
print(btc_price)

我已經刪除了我無意中在此處發布的錯誤信息。 不幸的是,帖子的 rest 不再像以前那樣有意義:/

Rest 的帖子沒有錯誤信息:

為此,您可以使用 unicorn-binance-websocket-api:

pip install unicorn-binance-websocket-api

創建到 Binance 的 websocket 連接:

from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager

binance_websocket_api_manager = BinanceWebSocketApiManager(exchange="binance.com")
binance_com_websocket_api_manager.create_stream('arr', '!userData', api_key=binance_com_api_key, api_secret=binance_com_api_secret)

還有 4 行打印接收到的數據記錄:

while True:
    oldest_stream_data_from_stream_buffer = binance_websocket_api_manager.pop_stream_data_from_stream_buffer()
    if oldest_stream_data_from_stream_buffer:
        print(oldest_stream_data_from_stream_buffer)

試試這個例子: https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/blob/master/example_userdata_stream.py

最新版本如python-binance v1.0.15 使用:

import asyncio

from binance import Client, ThreadedWebsocketManager, ThreadedDepthCacheManager

或者

from binance import AsyncClient, DepthCacheManager, BinanceSocketManager

client = Client (api_key, api_secret)

查看: https://python-binance.readthedocs.io/en/latest/#

@Daniel Droguett Quezada,應該是這個

async_client = await AsyncClient.create(api_key, api_secret) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM