python - websockets.exceptions.InvalidStatusCode : server rejected WebSocket connection: HTTP 400

标签 python ssl websocket header

我无法使用 websockets module 连接到 websocket .我有以下代码:

import asyncio, ssl
import websockets


headers= {
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'en-US,en;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'Upgrade',
        'Host': ...,
        'Origin': ...,
        'Pragma': 'no-cache',
        'Upgrade': 'websocket',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
    }

async def hello():
    uri = ...
    async with websockets.connect(ssl=0,
                                  extra_headers=headers,
                                  origin="*",
                                  uri = uri) as websocket:
        pass

asyncio.get_event_loop().run_until_complete(hello())
我得到以下异常:
Traceback (most recent call last):
  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\kr ws.py", line 30, in <module>
    asyncio.get_event_loop().run_until_complete(hello())
  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\kr ws.py", line 19, in hello
    async with websockets.connect(ssl=0,
  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\lib\site-packages\websockets\client.py", line 517, in __aenter__
    return await self
  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\lib\site-packages\websockets\client.py", line 542, in __await_impl__
    await protocol.handshake(
  File "C:\Users\AFMN\AppData\Local\Programs\Python\Python38-32\lib\site-packages\websockets\client.py", line 296, in handshake
    raise InvalidStatusCode(status_code)
websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 400
但问题是,我可以使用 websocket module 连接到 websocket ,但由于多种原因,我不想使用它。但如果有帮助,这里是使用 websocket 的代码:
import websocket, ssl
try:
    import thread
except ImportError:
    import _thread as thread
import time, msgpack

headers= {
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'en-US,en;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'Upgrade',
        'Host': ...,
        'Origin': ...,
        'Pragma': 'no-cache',
        'Upgrade': 'websocket',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
    }

def on_message(ws, message):
    ...

def on_error(ws, error):
    ...

def on_close(ws):
    ...

def on_open(ws):
    ...


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(...,
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close,
                              header=headers,
                              subprotocols=["binary", "base64"])
    ws.on_open = on_open
    ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
为什么我不能在第一个代码中连接到 websocket,而我可以使用另一个模块连接到同一个 websocket?
如何使用第一个连接?

最佳答案

你的网址是什么?看起来该服务器仅支持基于 TLS 的 websocket,因此您应该使用 wss://而不是 ws://

关于python - websockets.exceptions.InvalidStatusCode : server rejected WebSocket connection: HTTP 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62442562/

相关文章:

tomcat - 多级域 ssl 保护

java - 如何在 servlet 和套接字之间建立连接

python3 lxml 当前缀为空时如何查找节点?

python - 异步 DNS 解析器测试

c# - 服务器模式 SSL 必须使用具有关联私钥的证书 - 在 TLS 握手期间

php - CURL: SSL 证书失败,验证 CA 证书是否正常

python - 由 python 的子进程启动的进程不存在

Python SSL 请求和让我们加密证书

java - 使用 Spring Rabbitmq Stomp 的持久队列

Java Spring boot websocket与JS通信