mrkwjc
mrkwjc

Reputation: 1229

Python: why urllib.request.urlopen.read() never finishes download?

I have the code:

from urllib.request import urlopen
url = 'http://gmsh.info/bin/MacOSX/gmsh-4.5.2-MacOSX-sdk.tgz'
sdk = urlopen(url).read()

and the question: why this download never ends? Link is OK and it works in browsers. I tried to set some headers like this:

from urllib import request
req = request.Request(url)
req.add_header('user-agent', "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11")
sdk = request.urlopen(req).read()

but this didn't help. Any ideas?

Upvotes: 0

Views: 412

Answers (1)

Aniket singh
Aniket singh

Reputation: 138

this is because the file size is very big try downloading it into chunks.. as shown in example it will work.. import urllib.request filedata = urllib.request.urlopen('http://gmsh.info/bin/MacOSX/gmsh-4.5.2-MacOSX-sdk.tgz') CHUNK = 1 * 1024 with open('test.zip', 'wb') as f: while True: chunk = filedata.read(CHUNK) if not chunk: break f.write(chunk)

Upvotes: 1

Related Questions