【问题标题】:Limit uploaded file size in bottle [duplicate]限制瓶子中上传的文件大小[重复]
【发布时间】:2015-06-10 19:14:51
【问题描述】:

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

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

【问题讨论】:

    标签: python bottle


    【解决方案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

    【讨论】:

    • 感谢引用,但请不要从其他 SO 答案中复制/粘贴。将问题标记为重复。
    猜你喜欢
    • 1970-01-01
    • 2014-08-25
    • 2011-01-29
    • 2017-03-09
    • 2011-01-13
    • 1970-01-01
    • 2022-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多