Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Created February 6, 2019 01:44
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 CMCDragonkai/b2c8413e61856e929d3cefebf3516e25 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/b2c8413e61856e929d3cefebf3516e25 to your computer and use it in GitHub Desktop.
Download and Stream Hash #python
import hashlib
from urllib.request import urlopen
from urllib.error import HTTPError
file_url = 'https://somefile'
file_path = 'somefile'
sha256_hash = '751baea102ae63f0990607883ff819cfe18276d5116605673c5ff7e42e34e945'
hash = hashlib.sha256()
response = urlopen(file_url)
with open(file_path, mode='wb') as f:
for chunk in iter(lambda: response.read(65535), b''):
hash.update(chunk)
f.write(chunk)
if hash.hexdigest() != sha256_hash:
os.remove(file_path)
raise Exception('Upstream file changed!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment