限制上传的文件大小

时间:2015-06-10 11:04:36

标签: python bottle

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

我正在使用瓶子0.12和python 3.4。

1 个答案:

答案 0 :(得分:1)

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