gpt4 book ai didi

python - Binance python websocket - 无响应

转载 作者:行者123 更新时间:2023-12-02 19:18:08 29 4
gpt4 key购买 nike

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

最佳答案

我已经删除了无意中发布的错误信息。不幸的是,这篇文章的其余部分不再像以前那样有意义了:/

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

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

pip install unicorn-binance-websocket-api

创建与币安的 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/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/blob/master/example_userdata_stream.py

关于python - Binance python websocket - 无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63403862/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com