Limit uploaded file size in bottle

2.1k views Asked by At

How do I limit the size of files that users are able to upload to my server?

I'm using bottle 0.12 and python 3.4.

1

There are 1 answers

1
duck On BEST ANSWER
MAX_SIZE = 5 * 1024 * 1024
BUF_SIZE = 8192


data_blocks = []
byte_count = 0

buf = f.read(BUF_SIZE)
while buf:
    byte_count += len(buf)

    if byte_count > MAX_SIZE:
        # if you want to just truncate at (approximately) MAX_SIZE bytes:
        break
        # or, if you want to abort the call
        raise bottle.HTTPError(413, 'Request entity too large (max: {} bytes)'.format(MAX_SIZE))

    data_blocks.append(buf)
    buf = f.read(BUF_SIZE)

data = ''.join(data_blocks)

Python bottle - How to upload media files without DOSing the server