I'm having a little trouble handling chunked uploads from the server side using bottle.py. I'm using python requests and request-toolbelt for HTTP and upload progress tracking. I intend to use the script to upload large files hence the need for chunked uploads and avoiding reading the files in memory. I have slightly adapted the example shown in the requests-toolbelt docs and while I can see the upload progress data , I get a file with 0 size on the server side . Here's the code that i'm using.
#uploader.py
def create_callback(encoder):
encoder_length = len(encoder)
prog_bar = Bar(expected_size=encoder_length, filled_char='=')
def callback(monitor):
prog_bar.show(monitor.bytes_read)
return callback
if __name__ == "__main__":
m = MultipartEncoder(
fields={'testfile': ('test.txt', open('test.txt', 'rb'), 'text/plain')}
)
callback = create_callback(m)
m = MultipartEncoderMonitor(m, callback)
r = requests.post('http://127.0.0.1/upload', data=m,
headers={'Content-Type': m.content_type})
print r.status_code #prints 200
#server.py
@post("/upload")
def handle_upload():
print len(request.POST['testfile'].file.read()) #i always get zero
if __name__ == "__main__":
run(server='cherrypy', port="8080") # I still get zero even without the cherrypy server
I think I have followed the instructions but I just can't figure it out where I'm missing the point.
[Edit]
I have checked this post which describes how to patch the files back and relies on the Content-Range field in the headers but when I print out the request headers I can only see the content-Length