簡體   English   中英

握手時的python websocket 404

[英]python websocket 404 on Handshake

我使用python websocket-client向客戶端發送消息。

在客戶端,我有:

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

我在設置Web套接字時是否想念某些東西?

完整代碼: https//github.com/Homa/weatherApp

您正在后端中創建一個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()

如果您使用Flask-Sockets擴展,則在擴展中直接有一個用於gunicorn的websocket實現,這使得可以從以下命令行中啟動gunicorn -k flask_sockets.worker --bind 0.0.0.0:8000 app:app

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM