python - how to give a name to downloaded file with async tornado client -
i have python tornado code use files async
from tornado import ioloop , httpclient def get_file(url): http_client = httpclient.asynchttpclient() http_client.fetch(url,done) def done() print "done" url = "http://localhost/files/ldfhgdksgfjkdfadf/output.pdf" ioloop.ioloop.instance().start()
what happens file gets saved in current dir "output.pdf", there way asynchttp client specify file name? if call file name , save in different directory other current directory.
what reading response.body
in specified callback , write appropriate file, e.g.:
from tornado import ioloop, httpclient def get_file(url): http_client = httpclient.asynchttpclient() http_client.fetch(url, callback=done) def done(response): open("my_favorite_directory/my_favorite_filename.pdf", "w") f: f.write(response.body) print "done" get_file("http://samplepdf.com/sample.pdf") ioloop.ioloop.instance().start()
Comments
Post a Comment