Neda Homa
Neda Homa

Reputation: 4357

python websocket 404 on Handshake

I use python websocket-client to send message to client.

On client side I have:

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);
}

and on server side I have:

import websocket
ws = websocket.WebSocket()
ws = create_connection("ws://127.0.0.1:8000/ws", sslopt=  {"check_hostname": False})

I get this error: Handshake status 404

My guess is, there is a problem with web socket server: ws://127.0.0.1:8000/ws

Did I miss something in setting up my web socket?

Full code: https://github.com/Homa/weatherApp

Upvotes: 0

Views: 4701

Answers (2)

Dinesh Sharma
Dinesh Sharma

Reputation: 658

If you use Flask-Sockets extension, you have a websocket implementation for gunicorn directly in the extension which make it possible to start with the following command line gunicorn -k flask_sockets.worker --bind 0.0.0.0:8000 app:app

Upvotes: 0

Amin Etesamian
Amin Etesamian

Reputation: 3699

You are creating a websocket client in your backend. You have to create a websocket server and connect to in your javascript code.

ip install git+https://github.com/dpallot/simple-websocket-server.git and connect to it.

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()

Upvotes: 1

Related Questions