将上传的文件大小限制在瓶中

Limit uploaded file size in bottle

如何限制用户可以上传到我的服务器的文件大小?

我使用的是 0.12 瓶和 python 3.4。

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