python - 你如何将 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 客户端确实连接了。
有什么想法吗?
解决方案
推荐阅读
- bash - 捕获命令的输出而不将“\n”转换为文字换行符
- hyperledger-fabric - 如何在 Hyperledger composer 中为我的用例编写智能合约?
- c - 想一个代码来显示一个数字是素数还是非素数?
- java - 如何在 Java 中解析 JSON 字符串以获取包含在其中的 String anotherJSON
- c# - 如何在列表框中拖动项目时突出显示“悬停”项目
- java - 502 网关错误 Elastic Beanstalk Spring Boot
- android - 渲染图像的半透明主题活动问题
- swift - 二元运算符“*”不能应用于“CGRect”和“CGFloat”类型的操作数
- rust - 如何将任务添加到在另一个线程上运行的 Tokio 事件循环?
- c++ - 如果模板值可转换为布尔值,则在编译时禁用类方法