-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_download.py
executable file
·65 lines (55 loc) · 1.92 KB
/
file_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import urllib.request
import os
import sys
from http import HTTPStatus
from urllib.error import HTTPError
if len(sys.argv) != 3:
print(f'Usage: python {sys.argv[0]} <url> <file_path>')
sys.exit(1)
url = sys.argv[1]
file_path = sys.argv[2]
directory_path = os.path.dirname(file_path)
if not os.path.exists(directory_path):
print(f'Error: directory "{directory_path}" does not exist')
sys.exit(1)
if os.path.exists(file_path + '.etag'):
with open(file_path + '.etag', 'r') as f:
etag = f.read().strip()
headers = {'If-None-Match': etag}
else:
headers = {}
req = urllib.request.Request(url, headers=headers)
try:
response = urllib.request.urlopen(req)
except HTTPError as e:
if e.code == HTTPStatus.NOT_MODIFIED:
print('File has not been modified')
sys.exit(0)
else:
print(f'Download failed with HTTP {e.code}')
sys.exit(1)
if response.status == HTTPStatus.OK:
content_length = response.headers.get('Content-Length')
if content_length is not None:
file_size = int(content_length)
print(f'File size is {file_size} bytes')
else:
print('Could not determine file size')
with open(file_path, 'wb') as f:
f.write(response.read())
print(f'Downloaded file from "{url}" to "{file_path}" successfully')
downloaded_file_size = os.path.getsize(file_path)
if downloaded_file_size == file_size:
print('File size matches expected size')
else:
print(f'Error: downloaded file size ({downloaded_file_size} bytes) does not match expected size ({file_size} bytes)')
etag = response.headers.get('ETag')
if etag is not None:
with open(file_path + '.etag', 'w') as f:
f.write(etag)
print(f'Saved ETag {etag} to file {file_path}.etag')
else:
print(f'Response does not contain an ETag header')
else:
print(f'Download failed with HTTP {response.status}')
sys.exit(1)