Websocket升级失败:403禁止的AutobahnAsynci

2024-05-12 16:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Autobahn/Asyncio连接到websocket。我已经使用完全相同的代码连接到其他几个websocket,但是这个特定的websocket拒绝了我的尝试。连接升级失败,服务器响应403。我已经尝试用“websockets”包连接到这个websocket,并且已经成功了,所以在Autobahn实现中一定有一些默认设置导致了这个问题。你知道吗

我比较了websockets实现中成功的握手请求和autobahn实现中失败的握手请求:

WebSocket: 请求:

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

答复:

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

高速公路: 请求:

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

答复:

WebSocket connection upgrade failed (403 - Forbidden)

我甚至对autobahn实现进行了硬编码,以发送与“websockets”实现完全相同的GET请求,但没有起作用。你知道吗

高速公路代码:

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)

WebSocket代码:

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

我正在使用的高速公路代码已经成功地应用于其他几个websocket,那么我是否需要勾选某个特定于此websocket的标志?我怎么才能弄清楚呢?你知道吗


Tags: 代码comgetwswebsocketsv3secconnection