Bottle file upload and process

agnsaft picture agnsaft · Jan 12, 2013 · Viewed 12.7k times · Source

I am using Bottle for uploading rather large files. The idea is that when the file is uploaded, the web app run (and forget) a system command with the uploaded file-path as an argument. Except for starting the system command with the correct file-path as an argument I do not need to save the file, but I need to be certain that the file will be available until the process completes the processing.

I use the exact code described here: http://bottlepy.org/docs/dev/tutorial.html#post-form-data-and-file-uploads

My questions are:

  • Do bottle store uploaded file in memory or on a specific place on the disk (or perhaps like Flask, a bit of both)?
  • Will the uploaded file be directly available to other tools without .read() and then manually saving the bytes to a specified file on disk?
  • What would be the best way to start the system command with the file as an argument? Is it possible to just pass the path to an existing file directly?

Answer

IT Ninja picture IT Ninja · Jan 12, 2013

Ok, let's break this down.

The full code is:

HTML:

<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="text" name="name" />
  <input type="file" name="data" />
</form>

PYTHON CODE:

from bottle import route, request
@route('/upload', method='POST')
def do_upload():
    name = request.forms.name
    data = request.files.data
    if name and data and data.file:
        raw = data.file.read() # This is dangerous for big files
        filename = data.filename
        return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, len(raw))
    return "You missed a field."

(From the doc's you provided)

So, first of all, we can see that we first pull the information from the name and the data in the html form, and assign them to the variables name and data. Thats pretty straight forward. However, next we assign the variable raw to data.file.read(). This is basically taking all of the file uploaded into the variable raw. This being said, the entire file is in memory, which is why they put "This is dangerous for big files" as a comment next to the line.

This being said, if you wanted to save the file out to disk, you could do so (but be careful) using something like:

with open(filename,'w') as open_file:
    open_file.write(data.file.read())

As for your other questions:

1."What would be the best way to start the system command with the file as an argument? Is it possible to just pass the path to an existing file directly?"

You should see the subprocess module, specifically Popen: http://docs.python.org/2/library/subprocess.html#popen-constructor

2."Will the uploaded file be directly available to other tools without .read() and then manually saving the bytes to a specified file on disk?"

Yes, you can pass the file data around without saving it to disk, however, be warned that memory consumption is something to watch. However, if these "tools" are not in python, you may be dealing with pipes or subprocesses to pass the data to these "tools".