File: download_file.py

package info (click to toggle)
python-treq 24.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 592 kB
  • sloc: python: 4,895; makefile: 130
file content (13 lines) | stat: -rw-r--r-- 376 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
from twisted.internet.task import react

import treq


def download_file(reactor, url, destination_filename):
    destination = open(destination_filename, 'wb')
    d = treq.get(url, unbuffered=True)
    d.addCallback(treq.collect, destination.write)
    d.addBoth(lambda _: destination.close())
    return d

react(download_file, ['https://httpbin.org/get', 'download.txt'])