Binance Python Websocket-无响应

问题描述

我正在尝试使用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)

解决方法

使用livedataframe时,您正在中间使用第三方。更好的是,您直接连接到币安服务器!

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...