Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload with Tornado

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.
like image 860
missingfaktor Avatar asked Oct 09 '12 10:10

missingfaktor


People also ask

What is the use of sendfile in tornado?

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.

How can I serve multiple clients using Tornado downloadhandler?

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.

How do I serve large files using Tornado?

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.

Can Tornado keep up with nginx for serving large files?

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.


2 Answers

self.request.files should be fine. Here is an example.

like image 94
savruk Avatar answered Sep 28 '22 16:09

savruk


@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.

like image 34
missingfaktor Avatar answered Sep 28 '22 17:09

missingfaktor