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()
(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”.
IT Ninja
source share