Skip to content

Instantly share code, notes, and snippets.

@kaushikgandhi
Created February 20, 2024 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaushikgandhi/9ae6c82f9a1eff34e9e8aaf255262d0b to your computer and use it in GitHub Desktop.
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
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