Tornado Binary Part 2 Websites

I am trying to transfer binary data through websites, or rather compressed lines on top of websites. In my current setup, I use a tornado as a server with a websocket client that transmits binary data. Binary data is generated by compressing data using zlib . Both the client and server are as simple as they are, and are shown below.

Server:

 import tornado.websocket import tornado.httpserver import tornado.ioloop import tornado.web class WebSocketServer(tornado.websocket.WebSocketHandler): def open(self): print 'OPEN' def on_message(self, message): print 'len = {}'.format(len(message)) print 'GOT MESSAGE: {}'.format(message.decode('zlib')) def on_close(self): print 'CLOSE' app = tornado.web.Application([ (r'/', WebSocketServer) ]) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(9500) tornado.ioloop.IOLoop.instance().start() 

Client:

 import websocket host = 'localhost' port_ws = 9500 ws = websocket.create_connection('ws://{}:{}/'.format(host, port_ws)) message = 'this is my message'.encode('zlib') print 'Length of message is {}'.format(len(message)) ws.send(message) 

The client does not throw any errors, it prints that the message: Length of message is 24 . The message is encoded as str in accordance with the zlib standard. The server on the other end does not show that it has received any messages, it just realizes that the client has connected and then disconnected. Does anyone know where the problem is? I'm not sure if the problem lies in the tornado or in the websockets library. Any suggestions?


EDIT: in response to the comment below (@plg) I modified the scripts above to show that:

  • Uncoded messages can be sent by the client to the tornado server
  • Tornado can respond with encoded message

Server:

 import tornado.websocket import tornado.httpserver import tornado.ioloop import tornado.web class WebSocketServer(tornado.websocket.WebSocketHandler): def open(self): print 'OPEN' def on_message(self, message): print 'len = {}'.format(len(message)) print 'GOT MESSAGE: {}'.format(message) self.write_message(message.encode('zlib')) def on_close(self): print 'CLOSE' app = tornado.web.Application([ (r'/', WebSocketServer) ]) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(9500) tornado.ioloop.IOLoop.instance().start() 

Client:

 import websocket host = 'localhost' port_ws = 9500 ws = websocket.create_connection('ws://{}:{}/'.format(host, port_ws)) #message = 'this is my message'.encode('zlib') message = 'this is my message' print 'Length of message is {}'.format(len(message)) ws.send(message) assert ws.recv().decode('zlib') == message 

The system is operating normally. The statement does not cause an error. The decoded message corresponds to the send message. Therefore, I think there is a problem with:

  • Sending a coded message from a client
  • Tornado receiving encoded message

Honestly, I believe that the first option is more likely than a tornado. In my opinion, I believe that a tornado will warn me if an incoming message is incorrectly decoded in accordance with the websocket standard. Any other suggestions?


EDIT: More development of who is to blame. Instead of using my own server to reconnect and the fourth connection, I passed the connection to ws://echo.websocket.org/ . My testing application is shown as:

 import websocket host = 'localhost' port_ws = 9500 ws = websocket.create_connection('ws://echo.websocket.org/') message = 'this is my message' ws.send(message.encode('zlib')) got = ws.recv().decode('zlib') print 'GOT: {}'.format(got) assert got == message 

It actually passed the test, the data was received just fine. So, I think something is wrong with the tornado receiving the data?

+7
python tornado websocket
source share
2 answers

After looking at the source code of the websocket library websocket I found that by default it formats the packages as text. By changing the line:

 ws.send('message') # to: ws.send('message', opcode=websocket.ABNF.OPCODE_BINARY) # or better yet: ws.send_binary('message') 

The package will be sent just fine. Tornado, I think, simply ignored the fake binary packages, as they were marked as text and contained a binary file.

+3
source share

Thanks to this commit tornado supports websocket compression extensions.

0
source share

All Articles