Search code examples
pythonhttptornado

Tornado how to download a file?


i'm writing some kind of caching proxy-server with Python and Tornado, and i need to download different kinds of files from remote server, i use AsyncHTTPClient with callbacks to do it, and it works great on images, but when it comes to video or 3d models - callback function receives empty request.body, what am i doing wrong? My code looks like this:

def test_callback(self, response):
    print response.body

def get(self):
    client = tornado.httpclient.AsyncHTTPClient()
    client.fetch(remote_url, self.test_callback)

And it prints None, when i download video or 3d model.


Solution

  • I think this is because there is an error in the request or the request is timing out, so I'd suggest that you try the following:

    def test_callback(self, response):
        if response.error:
            print("Error:", response.error)
        else:
            print(response.body)
    

    This should give you more information. However, I think this is because the request keeps timing out. So, I'd suggest setting the connect_timeout and the request_timeout arguments in AsynHTTPClient to None.