forked from felipecruz/exemplos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_05_full_main.py
48 lines (35 loc) · 1.05 KB
/
04_05_full_main.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
# coding: utf-8
import io
import sys
import urllib.request as request
BUFF_SIZE = 1024
def download_length(response, output, length):
times = length // BUFF_SIZE
if length % BUFF_SIZE > 0:
times += 1
for time in range(times):
output.write(response.read(BUFF_SIZE))
print("Downloaded %d" % (((time * BUFF_SIZE)/length)*100))
def download(response, output):
total_downloaded = 0
while True:
data = response.read(BUFF_SIZE)
total_downloaded += len(data)
if not data:
break
output.write(data)
print('Downloaded {bytes}'.format(bytes=total_downloaded))
def main():
response = request.urlopen(sys.argv[1])
out_file = io.FileIO("saida.zip", mode="w")
content_length = response.getheader('Content-Length')
if content_length:
length = int(content_length)
download_length(response, out_file, length)
else:
download(response, out_file)
response.close()
out_file.close()
print("Finished")
if __name__ == "__main__":
main()