Skip to content

Instantly share code, notes, and snippets.

@shibukawa
Created May 13, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shibukawa/3b2d9ee8e052ae4b8a793c91014adc41 to your computer and use it in GitHub Desktop.
Save shibukawa/3b2d9ee8e052ae4b8a793c91014adc41 to your computer and use it in GitHub Desktop.
from six.moves import urllib
import os, io, inspect
import util
retry_count = 20
path = "https://snapshot.debian.org/archive/debian-security/20200424T130133Z/pool/updates/main/o/openjdk-11/openjdk-11-jdk-headless_11.0.7+10-3~deb10u1_amd64.deb"
def download(pkg_key, path, out_file):
res = urllib.request.urlopen(path)
remained_size = int(res.info().getheader("Content-Length"))
print("content-length: %d" % remained_size)
range_access_enabled = "bytes" in res.info().getheader("Accept-Ranges")
etag = res.info().getheader("ETag")
offset = 0
contents = []
downloaded = res.read()
contents.append(downloaded)
remained_size -= len(downloaded)
print("remained_size: %d" % remained_size)
if remained_size != 0:
if not range_access_enabled:
raise RuntimeError("Fail to downlod %s" % pkg_key)
offset += len(downloaded)
count = retry_count
while count > 0:
count -= 1
req = urllib.request.Request(path, headers={"Range": "bytes=%d-" % offset, "If-Range": etag})
res = urllib.request.urlopen(req)
if res.getcode() == 200:
contents = []
offset = 0
remained_size = int(res.info().getheader("Content-Length"))
downloaded = res.read()
contents.append(downloaded)
remained_size -= len(downloaded)
print("remained_size: %d" % remained_size)
if remained_size == 0:
break
with io.open(out_file, 'wb') as f:
for content in contents:
f.write(content)
def main():
out_file = os.path.join("file", util.encode_package_name("penjdk-11-jdk-headless.deb"))
download("openjdk", path, out_file)
actual_checksum = util.sha256_checksum(out_file)
print(actual_checksum)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment