Python開發【Tornado】:搭建檔案下載服務,pythontornado

來源:互聯網
上載者:User

Python開發【Tornado】:搭建檔案下載服務,pythontornado
Tornado 如何做檔案下載

要求:瀏覽器輸入url地址,直接彈窗提示下載 

 

Tornado服務端,搭建檔案下載服務

#!/usr/bin/env python# -*- coding:utf-8 -*-import tornado.ioloopimport tornado.webclass MainHandler(tornado.web.RequestHandler):    def get(self):        filename = self.get_argument('filename')        # http頭 瀏覽器自動識別為檔案下載        self.set_header('Content-Type', 'application/octet-stream')        # 下載時顯示的檔案名稱        self.set_header('Content-Disposition', 'attachment; filename=%s'%filename)        with open(filename, 'rb') as f:            while True:                data = f.read(1024)                if not data:                    break                self.write(data)        # # 記得有finish哦        self.finish()def make_app():    return tornado.web.Application([        (r"/", MainHandler),    ])if __name__ == "__main__":    app = make_app()    app.listen(8888)    tornado.ioloop.IOLoop.current().start()

瀏覽器驗證 輸入地址http://127.0.0.1:8888/?filename=meeting_welcome.wav:

 

補充:之前測試,一直在瀏覽器頁面顯示的是二進位字串,後來發現是沒有在http包裡面指定頭部類型

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.