Commit 48537682 authored by aknecht2's avatar aknecht2
Browse files

Fixed download function.

parent a728e126
......@@ -21,25 +21,30 @@ def downloadFile(url, localpath, urltype="ftp://", retries=3, overwrite=True, ch
for i in range(0, retries):
print "Attempt #%s, downloading %s\n" % (i + 1, url)
try:
hash_md5 = hashlib.md5()
conn = urllib2.urlopen(urltype + url)
with open(localpath, "w") as fwh:
response = urllib2.urlopen(urltype + url)
data = response.read()
if checkmd5:
if hashlib.md5(data).hexdigest() == md5:
fwh.write(data)
success = True
print "File saved successfully.\n"
else:
print "MD5 mismatch (input=[%s], hexdigest=[%s]). Retrying.\n" % (md5, hashlib.md5(data).hexdigest())
else:
fwh.write(data)
for chunk in iter(lambda: conn.read(4 * 1024), ""):
fwh.write(chunk)
hash_md5.update(chunk)
if checkmd5:
if hash_md5.hexdigest() == md5:
success = True
print "File saved successfully.\n"
except Exception as e:
else:
print "MD5 mismatch (input=[%s], hexdigest=[%s]). Retrying.\n" % (md5, hashlib.md5(data).hexdigest())
else:
success = True
print "File saved successfully.\n"
except urllib2.URLError as e:
print(traceback.format_exc())
overwrite = True
finally:
if conn:
conn.close()
if success:
return
else:
print "File already exists, skipping download.\n"
return
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment