Uploading and the process of uploading files to bottles

I use Bottle to upload quite large files. The idea is that when you download a file, the web application launches (and forgets) the system command with the downloaded file path as an argument. With the exception of running the system command with the correct file path, I don’t need to save the file as an argument, but I need to be sure that the file will be available until the process completes processing.

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

My questions:

  • Does the bottle store the downloaded file in memory or in a specific place on the disk (or, perhaps, like Flask, a little of both)?
  • Will the downloaded file be available directly to other tools without .read (), and then manually save the bytes to the specified file on disk?
  • What would be the best way to run a system command with a file as an argument? Is it possible to just pass the path to an existing file directly?
+7
source share
3 answers

Ok, let it break.

Full code:

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 provided document)

So, first of all, we see that first we extract information from name and data in the html form and assign them to the name and data variables. This is pretty straight forward. However, in the future, we assign the variable raw to data.file.read() . This basically takes the whole file loaded into the raw variable. In this case, the entire file is in memory, so they put "It is dangerous for large files" as a comment next to the line.

However, if you want to save the file to disk, you can do this (but be careful) using something like:

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

Regarding other issues:

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

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

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

Yes, you can transfer file data without saving to disk, but you should warn that memory consumption is what you need to see. However, if these “tools” are not in python, you can deal with pipes or subprocesses to transfer data to these “tools”.

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

Does not work

you can use

 data = request.files.data data.save(Path,overwrite=True) 
+4
source

The file will be processed by the program you are using. This means that your reading processes the connection (the file should not be in accordance with the wsgi specification)

+1
source

All Articles