首页 > 解决方案 > 你如何将 python 的 websocket-client 连接到 nodejs 套接字服务器

问题描述

一旦我尝试连接,我就会收到错误 --- 响应标头 --- 连接已经关闭。

我正在使用 pip install websocket-client 从 python 客户端连接我的 websocket 服务器正在 nodejs 上运行

我可以使用 javscript 从我的网络浏览器成功连接到我的服务器,但不能使用 python 客户端

Python客户端代码:

import websocket

def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    print("Open")


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://localhost:5000",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

错误:

--- 请求头 --- GET / HTTP/1.1 升级:websocket 主机:localhost:5000 来源:http://localhost:5000 Sec-WebSocket-Key:142ZQUrD0t0w2kwLIkLZag== Sec-WebSocket-Version:13 连接:升级

--- 响应头 ---

连接已经关闭。

注意:我的服务器正在运行,并且我的 javascript 客户端确实连接了。

有什么想法吗?

标签: pythonnode.jswebsocket

解决方案


推荐阅读