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