May-22-2020, 04:07 PM
Hello,
I'm stuck with a problem that I try to solve since a few days...
I'm a beginner at programming server/client networks.
I want to build a web application facilitating the following workflow:
1) receive and then process realtime data from a server via WebSocket API (python client)
2) send the processed data from the python client to another websocket server (python tornado)
3) send the data from the tornado websocket server to a javascript websocket client
4) presenting the data on a website
Everything works except for step 2. I have no idea how I can send data from a client to another server.
Maybe I have a fundamental misconception about how to build these kind of networks.
I post my code below so that you can have a glance at it.
I would highly appreciate if someone could shed some light on this!
Thanks and cheers!
lemon
![[Image: websocket.png]](https://i.postimg.cc/c4kYxjsc/websocket.png)
Python websocket client
var ws = new WebSocket("ws://localhost:8000/ws");
ws.onopen = function () {
ws.send("Client CONNECTED");
};
ws.onmessage = function (evt) {
document.getElementById("p1").innerHTML = evt.data;
};
ws.onclose = function () {
console.log("Client DISCONNECTED");
};
I'm stuck with a problem that I try to solve since a few days...

I'm a beginner at programming server/client networks.
I want to build a web application facilitating the following workflow:
1) receive and then process realtime data from a server via WebSocket API (python client)
2) send the processed data from the python client to another websocket server (python tornado)
3) send the data from the tornado websocket server to a javascript websocket client
4) presenting the data on a website
Everything works except for step 2. I have no idea how I can send data from a client to another server.
Maybe I have a fundamental misconception about how to build these kind of networks.
I post my code below so that you can have a glance at it.
I would highly appreciate if someone could shed some light on this!

Thanks and cheers!
lemon
![[Image: websocket.png]](https://i.postimg.cc/c4kYxjsc/websocket.png)
Python websocket client
from threading import Timer import websocket class WebsocketClient: def connect(self, wsURL="ws://echo.websocket.org"): self.__connect(wsURL) def closeWebsocket(self): self.ws.close() print("Timer end") def __connect(self, wsURL): self.ws = websocket.WebSocketApp( wsURL, on_message=self.__on_message, on_close=self.__on_close, on_open=self.__on_open, ) self.ws.run_forever() def __on_open(self): print("Websocket opened") self.ws.send("Websocket rocks!") def __on_message(self, msg): print("IN: ", msg) def __on_close(self): print("Websocket closed") def startClient(): ws = WebsocketClient() Timer(2.0, ws.closeWebsocket).start() ws.connect() print("Finished")Python Tornado websocket server
import os.path import tornado.web import tornado.websocket import tornado.httpserver from threading import Timer class Application(tornado.web.Application): def __init__(self): handlers = [ (r"/", IndexHandler), (r"/ws", WsHandler) ] settings = dict( template_path=os.path.join(os.path.dirname(__file__), "template"), static_path=os.path.join(os.path.dirname(__file__), "static"), ) tornado.web.Application.__init__(self, handlers, **settings) class IndexHandler(tornado.web.RequestHandler): def get(self): self.render("index.html") class WsHandler(tornado.websocket.WebSocketHandler): def open(self): self.write_message('Server CONNECTED') def on_message(self, message): print(message) def on_close(self): print('Server DISCONNECTED.') if __name__ == "__main__": app = Application() server = tornado.httpserver.HTTPServer(app) server.listen(8000) tornado.ioloop.IOLoop.instance().start()JavaScript websocket client
var ws = new WebSocket("ws://localhost:8000/ws");
ws.onopen = function () {
ws.send("Client CONNECTED");
};
ws.onmessage = function (evt) {
document.getElementById("p1").innerHTML = evt.data;
};
ws.onclose = function () {
console.log("Client DISCONNECTED");
};