握手时的python websocket 404
问题描述:
我使用python websocket-client将消息发送给客户端。握手时的python websocket 404
在客户端,我有:
var s = new WebSocket('http://' + location.host + ':8000/ws');
s.onopen = function(e) {
$("#connected3").html('open');
console.log(e)
}
s.onclose = function(e) {
$("#connected").html('close')
}
s.onmessage = function(e) {
$("#connected2").html(e.data);
}
,并在服务器端,我有:
import websocket
ws = websocket.WebSocket()
ws = create_connection("ws://127.0.0.1:8000/ws", sslopt= {"check_hostname": False})
我得到这个错误: 握手状态404
我的猜测是,有web套接字服务器的问题: ws://127.0.0.1:8000/ws
我错过了设置我的网络套接字的东西吗?
答
你在你的后台创建的WebSocket客户端。你必须创建一个websocket服务器并连接到你的javascript代码。
ip install git+https://github.com/dpallot/simple-websocket-server.git
并连接到它。
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
class SimpleEcho(WebSocket):
def handleMessage(self):
# echo message back to client
self.sendMessage(self.data)
def handleConnected(self):
print(self.address, 'connected')
def handleClose(self):
print(self.address, 'closed')
server = SimpleWebSocketServer('', 8000, SimpleEcho)
server.serveforever()
答
如果使用烧瓶套接字扩展,您可以直接在扩展gunicorn一个的WebSocket实现,这使得它可以先从以下命令行 gunicorn -k flask_sockets.worker --bind 0.0.0.0:8000 app:app