我正在尝试通过以下方式连接到币安服务:
wss://stream.binance.com:9443/ws/bnbbtc@kline_1m
我知道它可以工作,因为已经尝试过使用在线 Web 服务检查器,它注册监听服务器并毫无问题地接收 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
我真的很坚持这一点,如果有人可以提供帮助,我将不胜感激。