1

I have this code:

import shutil
import urllib.request
import zipfile

url = "http://wwww.some-file.com/my-file.zip"
file_name = url.split('/')[-1]

with urllib.request.urlopen(url) as response, open(file_name, 'wb') as out_file:
    shutil.copyfileobj(response, out_file)
    with zipfile.ZipFile(file_name) as zf:
        zf.extractall()

When trying the code I receive the following error:

urllib.error.HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found

I have been trying to combine solutions from here and here with no luck. Can anyone help me? Thanks

Victor M Herasme Perez
  • 1,945
  • 2
  • 12
  • 20

2 Answers2

0

I have used this solution for my project when I needed to download .gz files, maybe it will work for you.

from urllib.request import Request as urllibRequest

request = urllibRequest(url)
with open(file_name, 'wb') as output:
    output.write(urlopen(request).read())
0

I think the remote server can cause this issue. The server expects user headers and if there are no headers, then cause a redirect. As you mentioned, it is described here

Try to add user headers

Veniamin
  • 123
  • 7