regression towards the datascience

Tornado Error during WebSocket handshake


I was getting following exeption in WebSocket client, when trying to connect Tornado WebSocket server.

WebSocket connection to 'ws://localhost:5678/echo' failed: Error during WebSocket handshake: Unexpected response code: 403

and on the server-side log

WARNING:tornado.access:403 GET /echo (::1) 6.00ms

A simple Echo WebSocket server code.

from tornado import websocket, ioloop

class EchoWebSocket(websocket.WebSocketHandler):
    def open(self):
        print("WebSocket opened")

    def on_message(self, message):
        self.write_message(u"You said: " + message)

    def on_close(self):
        print("WebSocket closed")

app = web.Application([
    (r'/echo', EchoWebSocket),

if __name__ == '__main__':

It is because the server denies cross-orgin request. We should overide check_origin method to allow the clients to connect to the server.

def check_origin(self, origin):
    return True

The above snippet would allow all origin. However, to allow certain set of domains/sub-domains, appropriate validation has to be implemented.

Hope this helps.