Websocket upgrade failure 403 forbidden

I’m trying to connect to a websocket using Autobahn/Asyncio. I have connected to several other websockets using the exact same code, but this particular websocket is rejecting my attempts. I’m getting a connection upgrade failure and the server is responding with a 403. I have tried to connect to this websocket with the ‘websockets’ package and that has been successful, so there must be some default setting in the Autobahn implementation that is causing the problem.

I have compared the successful handshake request from the websockets implementation with the unsuccessful handshake request from the autobahn implementation:

websockets: request:

GET /ws/v3 HTTP/1.1
Host: real.okex.com:10442
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: eTj8gJmhJGTj868ObmO2qg==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
User-Agent: Python/3.6 websockets/8.0

response:

Server: nginx
Date: Tue, 09 Jul 2019 20:49:33 GMT
Connection: upgrade
upgrade: websocket
sec-websocket-acept: NJxn9G+1AJaYlHY1n1/Iy6P+2R8=

autobahn: request:

GET /ws/v3 HTTP/1.1
User-Agent: AutobahnPython/18.11.1
Host: real.okex.com:10442
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: QI5/tydGJRB6/LMDXkQ1dg==
Sec-WebSocket-Version: 13

response:

WebSocket connection upgrade failed (403 - Forbidden)

I have even hardcoded the autobahn implementation to send the exact same GET request as the ‘websockets’ implementation but that didn’t work.

Autobahn code:

async def connect_to_OKEX():
while True:
try:
global OKEX_transport
ws_endpoint = ‘wss://real.okex.com:10442/ws/v3’
factory = WebSocketClientFactory(ws_endpoint)
factory.protocol = OKEX_Protocol
coro = loop.create_connection(factory, ‘www.okex.com’, 443, ssl=True)
print(“running {}”.format(coro))
OKEX_transport, proto = await coro
print(“proto {}”.format(proto))
break
except Exception as e:
logger.exception(‘OKEX error connecting’)
await asyncio.sleep(1)

websockets code:

async def subscribe_without_login(‘wss://real.okex.com:10442/ws/v3’, channels):
async with websockets.connect(url) as websocket:

The autobahn code that I am using has worked successfully for several other websockets, so is there perhaps some flag that I need to tick which is particular to this websocket? How could I go about figuring that out?

Should probably be 10442 not 443 for the port, right?

That’s a reasonable guess, but no that’s not it. If you change the port to 10442 the request just times out.

Then maybe try changing the port in the URL? It seems odd to have a different port in the URL than what you’re actually connecting to?

I agree, it is strange, but it’s not the problem. I tried changing the port to 443 in the URL and it didn’t work. Besides, that same URL worked in the “websockets” implementation. I don’t get what could be different about the “websockets” connection attempt from the autobahn connection attempt.

I agree it’s weird. I won’t have time to look in detail until tomorrow afternoon, but can you use triple-backticks and re-paste your code with proper indenting? (I did this to the other bits of your post already)

Does your (autobahn) code work to connect to other websocket servers?

Sure, no problem. Yes the autobahn code works to connect to other servers. Here is the re-formatted code and an example of the working autobahn code:

re-pasted Autobahn code:

async def connect_to_OKEX():
    while True:
        try:
            global OKEX_transport
            ws_endpoint = 'wss://real.okex.com:10442/ws/v3'
            factory = WebSocketClientFactory(ws_endpoint)
            factory.protocol = OKEX_Protocol
            coro = loop.create_connection(factory, 'www.okex.com', 443, ssl=True)
            print("running {}".format(coro))
            OKEX_transport, proto = await coro
            print("proto {}".format(proto))
            break
        except Exception as e:
            logger.exception('OKEX error connecting')
            await asyncio.sleep(1)

re-pasted websockets code:

async def subscribe_without_login(‘wss://real.okex.com:10442/ws/v3’, channels):
    async with websockets.connect(url) as websocket:

successful autobahn example:

async def connect_to_GDAX():
    while True:
        try:
            global GDAX_transport
            ws_endpoint = 'wss://ws-feed.prime.coinbase.com'
            factory = WebSocketClientFactory(ws_endpoint)
            factory.protocol = GDAX_Protocol
            coro = loop.create_connection(factory, 'ws-feed.prime.coinbase.com', 443, ssl=True)
            print("running {}".format(coro))
            GDAX_transport, GDAX_proto = await coro
            print("proto {}".format(GDAX_proto))
            break
        except Exception as e:
            logger.exception('GDAX error connecting')
            await asyncio.sleep(1)

Hi meejah,

Did you have a minute to look at this issue?

Thanks