How do I access the uploaded file in Tornado, when using a put
request?
@require_basic_auth
class UploadFile(tornado.web.RequestHandler):
def put(self, params):
path = calculate_path(params)
# TODO: create an empty binary file at path and then copy
# the request input stream to it.
What's sendfile? sendfile is a function available on Linux (and Unix) which allows copying file to a socket at kernel level. This is far more faster than what we're doing - reading the file and writing to socket. While Python supports this using os.sendfile, Tornado doesn't. But there's an issue on Github about adding this support to Tornado.
Now that we've made the DownloadHandler asynchronous, we can serve multiple clients in a non-blocking way. Even if a few different users want to download different files, at the same time, our server won't block. Tornado isn't meant for serving large data. You should, when you have the option, use Nginx to serve large files.
We need to take care of two things while serving large files using Tornado: To do that, we'll have to read and send the files in chunks. What that means is we'll read a few megabytes and send them, then read the next few megabytes and send them and we'll keep doing that until we've read and sent the whole file.
Tornado isn't meant for serving large data. You should, when you have the option, use Nginx to serve large files. The benchmarks clearly show that. It's quite apparent that Tornado can't keep up with Nginx. What's sendfile? sendfile is a function available on Linux (and Unix) which allows copying file to a socket at kernel level.
self.request.files
should be fine. Here is an example.
@require_basic_auth
class UploadFile(tornado.web.RequestHandler):
def put(self, params):
path = calculate_path(params)
with open(path, 'wb') as out:
body = self.request.get_argument('data')
out.write(bytes(body, 'utf8'))
...was what I needed.
Found on some ActiveState pages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With