Is there a way to set an uploaded file size limit in tornado?
Asked Answered
S

3

9

I am using tornado to make an image processing RESTful service, which is accepting images uploaded by general HTTP means like multipart/form-data. I then access them in handlers using self.request.files.

It could be that an adversary will try to upload a huge file to break down a service. Is there any way to tell tornado an uploaded file size limit, exceeding which file should be discarded and error HTTP status should be set?

Sforza answered 5/10, 2012 at 12:43 Comment(0)
E
11

I've tried this, It works! max_buffer_size default value is 100 M.

import tornado.httpserver

app = tornado.web.Application([
    (r'/upload/', UploadFileHandler),
])

server = tornado.httpserver.HTTPServer(app, max_buffer_size=10485760000)  # 10G
server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Esque answered 21/4, 2016 at 1:25 Comment(0)
M
4

You can pass a max_buffer_size parameter when creating the server. For example to allow for 160MB uploads"

HTTPServer(app, max_buffer_size=167772160)
Matchboard answered 1/3, 2016 at 20:7 Comment(0)
J
2

You have to configure this on the web server. For example, using nginx:

client_max_body_size 50M;

Edit: The stream that HttpServer uses has a max_buffer_size property. HttpServer will not accept uploads bigger than this. The default value for it is 100MB. It seems to me that HttpServer simply closes the connection instead of sending a HTTP response when this limit is reached.

Jotting answered 5/10, 2012 at 12:46 Comment(2)
So the question is how to configure this option for tornado.Sforza
Sorry, I have edited my question to contain slightly more useful information.Jotting

© 2022 - 2024 — McMap. All rights reserved.