首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能流到python-binance

不能流到python-binance
EN

Stack Overflow用户
提问于 2021-12-24 16:20:28
回答 1查看 1.2K关注 0票数 0

我试着在一些行上做一个简单的流,但是我得到了一个错误消息。我可以注意到,根据版本的不同,websocket调用是不同的。我在1.0.15版本

你能帮我一下吗?

代码语言:javascript
复制
# Importing libraries
from binance.client import Client
import configparser
from binance.streams import ThreadedWebsocketManager


# Loading keys from config file
config = configparser.ConfigParser()
config.read_file(open('secret.cfg'))
actual_api_key = config.get('BINANCE', 'ACTUAL_API_KEY')
actual_secret_key = config.get('BINANCE', 'ACTUAL_SECRET_KEY')

client = Client(actual_api_key, actual_secret_key, tld="com")


def stream_data(msg):
    """
    Function to process the received messages
    param msg: input message
    """
    print(f"message type: {msg['e']}")
    print(f"close price: {msg['c']}")
    print(f"best ask price: {msg['a']}")
    print(f"best bid price: {msg['b']}")
    print("---------------------------")


# Starting the WebSocket
twm = ThreadedWebsocketManager()
twm.start()

#Subscribe to the stream
twm.start_symbol_miniticker_socket(callback=stream_data, symbol="BTCUSDT")

# Stopping websocket
twm.stop()

在这里,错误消息:

取消read_loop

进程已完成,退出代码为0

EN

回答 1

Stack Overflow用户

发布于 2022-01-20 02:24:09

应处理下列各部分:

在这个场景中,

  • ThreadedWebsocketManager不需要使用API Key/Secret
  • client调用初始化
  • ,在启动流之后,您应该在流发生

的情况下加入twm。

代码语言:javascript
复制
# Importing libraries
from binance.client import Client
import configparser
from binance.streams import ThreadedWebsocketManager

# Loading keys from config file
config = configparser.ConfigParser()
config.read_file(open('secret.cfg'))
actual_api_key = config.get('BINANCE', 'ACTUAL_API_KEY')
actual_secret_key = config.get('BINANCE', 'ACTUAL_SECRET_KEY')

# This is not REQUIRED
# client = Client(actual_api_key, actual_secret_key, tld="com")


def stream_data(msg):
    """
    Function to process the received messages
    param msg: input message
    """
    print(f"message type: {msg['e']}")
    print(f"close price: {msg['c']}")
    print(f"best ask price: {msg['a']}")
    print(f"best bid price: {msg['b']}")
    print("---------------------------")


# Starting the WebSocket
twm = ThreadedWebsocketManager(api_key=actual_api_key, api_secret=actual_secret_key)
twm.start()

# Define a stream or two
streams = ['ethusdt@kline_1m', 'btcusdt@kline_1m']

# Starting Binance multiplex_socket on ThreadedWebsocketManager
twm.start_multiplex_socket(callback=stream_data, streams=streams)

timeout = 0.1 # seconds
while True and twm.is_alive():
    twm.join(timeout=timeout) # Give twm a call to process the streaming
    # Do other stuff here

# Stopping websocket
twm.stop()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70474836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档