Answer the question
In order to leave comments, you need to log in
Why is it throwing an HTTP Error 301?
There is a script that downloads files and resumes downloading if the server drops the connection. But there is an error that often appears after 90-95% loading: urllib.error.HTTPError: HTTP Error 301: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was: Moved Permanently
cj = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
import urllib.request
from http.cookiejar import CookieJar
req = urllib.request.Request(URL)
cj = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
u = opener.open(req)
meta = u.info()
f = open(NAME, 'wb')
file_size = int(meta.get_all("Content-Length")[0])
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer and file_size_dl < file_size:
print("\n\nСервер сбросил соединение. Возобновление загрузки.\n\n")
req.headers["Range"] = "bytes=%s-%s" %(file_size_dl, file_size)
u = urllib.request.urlopen(req)
elif not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
sys.stdout.write("\rЗагружено: %3.2f%%, %d байт" % (file_size_dl * 100. / file_size, file_size_dl))
sys.stdout.flush()
f.close()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question