Staging
v0.5.1
https://github.com/python/cpython
Revision 1a3eb125dc07a28a5af62446778ed7cca95ed3da authored by Miss Islington (bot) on 05 September 2018, 14:45:57 UTC, committed by Victor Stinner on 05 September 2018, 14:45:57 UTC
(cherry picked from commit 7d81e8f5995df6980a1a02923e224a481375f130)
(cherry picked from commit 20a8392cec2967f15ae81633c1775645b3ca40da)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent e2c1657
Raw File
Tip revision: 1a3eb125dc07a28a5af62446778ed7cca95ed3da authored by Miss Islington (bot) on 05 September 2018, 14:45:57 UTC
[3.7] bpo-26544: Get rid of dependence from distutils in platform. (GH-8356) (GH-8970) (GH-9061)
Tip revision: 1a3eb12
urlretrieve.py
# Simple Python script to download a file. Used as a fallback
# when other more reliable methods fail.
#
from __future__ import print_function
import sys

try:
    from requests import get
except ImportError:
    try:
        from urllib.request import urlretrieve
        USING = "urllib.request.urlretrieve"
    except ImportError:
        try:
            from urllib import urlretrieve
            USING = "urllib.retrieve"
        except ImportError:
            print("Python at", sys.executable, "is not suitable",
                  "for downloading files.", file=sys.stderr)
            sys.exit(2)
else:
    USING = "requests.get"

    def urlretrieve(url, filename):
        r = get(url, stream=True)
        r.raise_for_status()
        with open(filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024):
                f.write(chunk)
        return filename

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage: urlretrieve.py [url] [filename]", file=sys.stderr)
        sys.exit(1)
    URL = sys.argv[1]
    FILENAME = sys.argv[2]
    print("Downloading from", URL, "to", FILENAME, "using", USING)
    urlretrieve(URL, FILENAME)
back to top