diff --git a/cget/util.py b/cget/util.py index b41ac1a..34a4d56 100644 --- a/cget/util.py +++ b/cget/util.py @@ -1,4 +1,4 @@ -import click, os, sys, shutil, json, six, hashlib, ssl +import click, os, sys, shutil, json, six, hashlib if sys.version_info[0] < 3: try: @@ -16,7 +16,7 @@ else: import subprocess -from six.moves.urllib import request +from six.moves.urllib import request, error def to_bool(value): x = str(value).lower() @@ -209,12 +209,6 @@ def symlink_to(src, dst_dir): os.symlink(src, target) return target -class CGetURLOpener(request.FancyURLopener): - def http_error_default(self, url, fp, errcode, errmsg, headers): - if errcode >= 400: - raise BuildError("Download failed with error {0} for: {1}".format(errcode, url)) - return request.FancyURLopener.http_error_default(self, url, fp, errcode, errmsg, headers) - def download_to(url, download_dir, insecure=False): name = url.split('/')[-1] file = os.path.join(download_dir, name) @@ -227,9 +221,12 @@ def hook(count, block_size, total_size): # Hack because we can't set the position bar.pos = percent bar.update(0) - context = None - if insecure: context = ssl._create_unverified_context() - CGetURLOpener(context=context).retrieve(url, filename=file, reporthook=hook, data=None) + try: + request.urlretrieve(url, filename=file, reporthook=hook, data=None) + except error.HTTPError as e: + if e.status >= 400: + raise BuildError("Download failed with error {0} for: {1}".format(e.status, url)) + raise bar.update(bar_len) if not os.path.exists(file): raise BuildError("Download failed for: {0}".format(url))