how to limit tornado websocket message size

前端 未结 2 1519
离开以前
离开以前 2021-01-15 00:27

I have written a websocket server in tornado and on_message method is called when a message is received. The problem is, the message size is unlimited by defual

相关标签:
2条回答
  • 2021-01-15 00:48

    take a look at the documentation here:

    http://www.tornadoweb.org/en/stable/http1connection.html#tornado.http1connection.HTTP1Connection.set_max_body_size

    To paraphrase for future proofing the link:

    set_max_body_size(max_body_size)[source]

    Sets the body size limit for a single request.

    Overrides the value from HTTP1ConnectionParameters.

    0 讨论(0)
  • 2021-01-15 01:10

    Since version 4.5 Tornado will close the connection automatically if it receives more than 10 MiB of data in a single websocket frame (message). So, you don't have to worry about someone sending huge data in a single message. You can see this in the source code. It's also mentioned in the docs of WebsocketHandler in the second-last paragraph.

    If you'd like to change the default frame limit you can pass your Application class an argument called websocket_max_message_size with the size in bytes.

    app = tornado.web.Application(
          # your handlers etc,
          websocket_max_message_size=128
    )
    
    0 讨论(0)
提交回复
热议问题