SSL error: wrong version number (in ssl3_get_record)

Hello everyone.

I’m trying to use a new cryptocurrency exchange called Bybit. As a simple starting point I tried the following code just to check if there is something wrong with WebSocket connections in general.

import sys
from twisted.python import log
from twisted.internet import reactor, ssl
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS

class Protocol(WebSocketClientProtocol):

    def onOpen(self):
        print("Connected!")
   
    def onMessage(self, payload, isBinary):
        print(payload.decode("utf8"))

if __name__ == "__main__":
    log.startLogging(sys.stdout)
    #wss://stream.binance.com/stream
    factory = WebSocketClientFactory("wss://stream.bybit.com/realtime")
    factory.protocol = Protocol
    contextFactory = ssl.ClientContextFactory()
    connectWS(factory, contextFactory)
    reactor.run()

But when I try that code I get the following error: SSL error: wrong version number (in ssl3_get_record)

When I replace the URL with the URL of Binance (in the commented line) everything works fine.

I tried the same code on different systems with different operating systems but that solved the issue. Then I tried to use a different pyOpenSSL version but that didn’t work either. :confused:

Does anyone know what the exact issue could be?

Thank you for every help!

I think you want to use optionsForClientTLS() for nearly every case. So replace the second-last line with connectWS(factory, ssl.optionsForClientTLS("stream.bybit.com")).

Twisted’s TLS support has been around for a while through a few iterations, but the above is the newest (and safest!) API.

1 Like

Wow. How long I was struggling to get that code to run…
That works amazing (also with Binance) and if that is also the safest API that is even better.

Thank you very much. :slight_smile: