-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPyHTTPDown.py
More file actions
41 lines (34 loc) · 1.24 KB
/
Copy pathPyHTTPDown.py
File metadata and controls
41 lines (34 loc) · 1.24 KB
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
import urllib.request, urllib.parse, urllib.error
import os
TARGET_URL = 'http://python.org/ftp/python/2.7.4/'
TARGET_FILE = 'Python-2.7.4.tgz'
class CustomURLOpener(urllib.request.FancyURLopener):
def http_error_206(self, url, fp, errcode, errmsg, headers, data=None):
pass
def resume_download():
file_exists = False
CustomURLClass = CustomURLOpener()
if os.path.exists(TARGET_FILE):
out_file = open(TARGET_FILE, "ab")
file_exists = os.path.getsize(TARGET_FILE)
CustomURLClass.addheader("range", "bytes=%s-" % (file_exists))
else:
out_file = open(TARGET_FILE, "wb")
web_page = CustomURLClass.open(TARGET_URL + TARGET_FILE)
if int(web_page.headers['Content-Length']) == file_exists:
loop = 0
print("File already downloaded!")
byte_count = 0
while True:
data = web_page.read(8192)
if not data:
break
out_file.write(data)
byte_count = byte_count + len(data)
web_page.close()
out_file.close()
for k, v in list(web_page.headers.items()):
print(k, "=", v)
print("File copied", byte_count, "bytes from", web_page.url)
if __name__ == '__main__':
resume_download()