【问题标题】:Binance python websocket - no responseBinance python websocket - 没有响应
【发布时间】:2020-12-03 19:35:04
【问题描述】:

我正在尝试使用 python-binance 并按照本教程运行 binance websocket api 的示例代码:https://livedataframe.com/live-cryptocurrency-data-python-tutorial/

不幸的是,我无法让它工作。 websocket 只是不响应导致空终端没有打印任何内容的结果。

我正在使用 python 3.7.8、PyCharm IDE、Windows 10

请帮忙,谢谢!

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)

【问题讨论】:

    标签: python websocket binance


    【解决方案1】:

    @Daniel Droguett Quezada,应该是这个

    async_client = await AsyncClient.create(api_key, api_secret) 
    

    【讨论】:

      【解决方案2】:

      最新版如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/#

      【讨论】:

        【解决方案3】:

        我已删除无意中在此处发布的错误信息。不幸的是,帖子的其余部分不再像以前那样有意义:/

        帖子的其余部分没有错误信息:

        为此,您可以使用 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

        【讨论】:

        • Oliver,他在 livedataframe 教程中使用的库是 python-binance 它是一个非官方库,但它直接连接到币安服务器,就像 unicorn-binance-websocket-api 你的答案看起来不错,但第一行您的帖子似乎是虚假信息。那么也许你可以删除第 3 方声明吗?除非我在这里错了,否则请澄清。
        • Livedataframe 还提供了一个单独的模块pypi.org/project/livedataframe,不知怎的让我感到困惑。抱歉信息有误!
        猜你喜欢
        • 1970-01-01
        • 2021-04-15
        • 1970-01-01
        • 2016-03-28
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多