gpt4 book ai didi

python - websocket._exceptions.WebSocketBadStatusException : Handshake status 404 NOT FOUND

转载 作者:行者123 更新时间:2023-12-03 16:51:08 40 4
gpt4 key购买 nike

我正在尝试从 python 脚本与 Flask websocket 通信。但我收到以下异常。

Traceback (most recent call last):
File "client.py", line 5, in <module>
socket = create_connection("ws://127.0.0.1:5000")
File "C:\Users\anyms\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websocket\_core.py", line 514, in create_connection
websock.connect(url, **options)
File "C:\Users\anyms\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websocket\_core.py", line 226, in connect
self.handshake_response = handshake(self.sock, *addrs, **options)
File "C:\Users\anyms\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websocket\_handshake.py", line 79, in handshake
status, resp = _get_resp_headers(sock)
File "C:\Users\anyms\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websocket\_handshake.py", line 160, in _get_resp_headers
raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers)
websocket._exceptions.WebSocketBadStatusException: Handshake status 404 NOT FOUND

app.py

from flask import Flask
from flask_socketio import SocketIO, send


app = Flask(__name__)
app.config["SECRET_KEY"] = "uhjsbajksHGhksajjf^&*8*()"
socketio = SocketIO(app)


@socketio.on("message")
def message(msg):
print("Message received: {}".format(msg))
send(msg, broadcast=True)


@socketio.on("connect")
def connect():
print("Connected!")


if __name__ == "__main__":
socketio.run(app)

客户端.py

#!/usr/bin/python

from websocket import create_connection

socket = create_connection("ws://127.0.0.1:5000")
socket.send("hello, world")

result = socket.recv()
print("Received: {}".format(result))

socket.close()

如果我编写一个 HTML 页面作为客户端,它工作正常,但我无法从我的 python 脚本连接。

当我使用 ws://echo.websocket.org它工作正常,我很困惑,我是 websocket 编程的新手。

最佳答案

现在 1 岁了,但基本上 websockets 和 socket.io 是创建套接字的两种不同方法。它们不兼容。您有一个 socket.io 服务器,但有一个 websocket 客户端。更改其中一个以匹配另一个,它应该可以工作。

关于python - websocket._exceptions.WebSocketBadStatusException : Handshake status 404 NOT FOUND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55954664/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com