Created
February 6, 2019 01:44
-
-
Save CMCDragonkai/b2c8413e61856e929d3cefebf3516e25 to your computer and use it in GitHub Desktop.
Download and Stream Hash #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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