【问题标题】:How to connect to binance websocket service using autobahn with asyncio如何使用带有 asyncio 的高速公路连接到 binance websocket 服务
【发布时间】:2018-10-06 03:41:59
【问题描述】:

我正在尝试通过以下方式连接到币安服务:

wss://stream.binance.com:9443/ws/bnbbtc@kline_1m

我知道它可以工作,因为我尝试过使用在线网络服务检查器,它注册监听服务器并毫无问题地接收 100 万支蜡烛。

正如我所见,当我将路径添加到主机时,问题就出现了。如果我不添加路径“/ws/bnbbtc@kline_1m”,它会连接但会立即出现错误:

WebSocket connection closed: connection was closed uncleanly (WebSocket connection upgrade failed (400 - BadRequest))

这是我正在使用的代码,主要摘自示例:

from autobahn.asyncio.websocket import WebSocketClientProtocol, WebSocketClientFactory

class MyClientProtocol(WebSocketClientProtocol):

    def onConnect(self, response):
        print("Server connected: {0}".format(response.peer))

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        if isBinary:
            print("Binary message received: {0} bytes".format(len(payload)))
        else:
            print("Text message received: {0}".format(payload.decode('utf8')))

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {0}".format(reason))


if __name__ == '__main__':

    import asyncio

    factory = WebSocketClientFactory()
    factory.protocol = MyClientProtocol

    loop = asyncio.get_event_loop()
    coro = loop.create_connection(factory,"stream.binance.com/ws/bnbbtc@kline_1m", 9443)
    loop.run_until_complete(coro)
    loop.run_forever()
loop.close()

使用这个我从 getaddrinfo 收到以下错误:

for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11003] getaddrinfo failed

我真的很坚持,如果有人可以提供帮助,我将不胜感激。

【问题讨论】:

    标签: python-3.x websocket python-asyncio autobahn binance


    【解决方案1】:

    好吧,经过几个小时的尝试修复非常明显,我将代码留在这里供任何人检查他们是否需要:

    factory = WebSocketClientFactory("wss://stream.binance.com:9443/ws/bnbbtc@kline_1m")
    factory.protocol = MyClientProtocol
    
    loop = asyncio.get_event_loop()
    coro = loop.create_connection(factory,"stream.binance.com", 9443, ssl=True)
    loop.run_until_complete(coro)
    loop.run_forever()
    

    我错过了 ssl=True 部分。

    【讨论】:

    • 你是在 celery worker 里面运行这个吗?您如何处理服务器在从流中接收数据时对客户端无响应的可能性,我假设所有这些都在主进程主线程上运行
    【解决方案2】:

    我知道,已经晚了,但这也可以解决问题,您必须使用 4 行代码:

    $ pip install unicorn-binance-websocket-api

    from unicorn_binance_websocket_api.unicorn_binance_websocket_api_manager import BinanceWebSocketApiManager
    from unicorn_binance_websocket_api_process_streams import BinanceWebSocketApiProcessStreams
    
    binance_websocket_api_manager = BinanceWebSocketApiManager(BinanceWebSocketApiProcessStreams.process_stream_data)
    binance_websocket_api_manager.create_stream('kline_1m', 'bnbbtc')
    

    您使用“create_stream”创建的每个 websocket 都将在单独的线程中创建,并使用 asyncio 编写为协程。

    要处理接收到的数据,您可以使用此模板:https://github.com/unicorn-data-analysis/unicorn-binance-websocket-api/blob/master/unicorn_binance_websocket_api_process_streams.py

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多