Issue
I'm trying to connect to a binance service through:
wss://stream.binance.com:9443/ws/bnbbtc@kline_1m
I know it works because have tried with an online webservice checker and it registers to listen to the server and receives 1m candles without problem.
As I have seen the problem comes when I add the path to the host. If I don't add the path "/ws/bnbbtc@kline_1m" it connects but inmediatelly with error:
WebSocket connection closed: connection was closed uncleanly (WebSocket connection upgrade failed (400 - BadRequest))
This is the code I'm using, mainly extracted from the examples:
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()
Using this I get the following error from getaddrinfo:
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11003] getaddrinfo failed
I'm really stuck with this, if anyone could help I would really appreciate it.
Solution
Well, After some hours of trying the fix was pretty obvious, I will leave here the code for anyone to check if they need:
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()
I was missing the ssl=True part.
Answered By - Notbad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.