Created
February 20, 2024 09:58
-
-
Save kaushikgandhi/9ae6c82f9a1eff34e9e8aaf255262d0b to your computer and use it in GitHub Desktop.
Download a File Asynchronously From Web And Serve to Download Using Python Tornado Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import tornado | |
from tornado import gen | |
from tornado.httpclient import AsyncHTTPClient | |
class MainHandler(tornado.web.RequestHandler): | |
@gen.coroutine | |
def get(self): | |
url = self.get_argument('url') | |
filename = self.get_argument('filename') | |
self.set_header('Content-Type', 'application/octet-stream') | |
self.set_header('Content-Disposition', 'attachment; filename=%s' % filename) | |
self.flush() | |
response = yield AsyncHTTPClient().fetch(url) | |
self.finish(response.body) | |
def make_app(): | |
return tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
async def main(): | |
app = make_app() | |
app.listen(8888) | |
await asyncio.Event().wait() | |
if __name__ == '__main__': | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment