tags:

views:

95

answers:

3

I'm attempting to download a zip file using this code:

o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )

#login
p = urllib.urlencode( { usernameField: usernameVal, passField: passVal } )
f = o.open(authUrl,  p )
data = f.read()
print data
f.close()

#download file
f = o.open(remoteFileUrl)
localFile = open(localFile, "wb")
localFile.write(f.read())
f.close()

I am getting some binary data, but the size of the file I "downloaded" is too small and is not a valid zip file. Am I not retrieving the zip file properly? The HTTP response header for f = o.open(remoteFileUrl) is shown below. I don't know if special processing is needed to handle this response:

HTTP/1.1 200 OK Server:
Apache-Coyote/1.1 Pragma: private
Cache-Control: must-revalidate
Expires: Tue, 31 Dec 1997 23:59:59 GMT
Content-Disposition: inline;
filename="files.zip";
Content-Type: application/zip
Transfer-Encoding: chunked

+3  A: 

f.read() doesn't necessarily read the whole file, but just a packet of it (which might be the whole file if it's small, but won't be for a large file).

You need to loop over the packets like this:

while 1:
   packet = f.read()
   if not packet:
      break
   localFile.write(packet)
f.close()

f.read() returns an empty packet to signify that you've read the whole file.

RichieHindle
I would be curious where in the documentation you found this
Xavier Combelle
http://docs.python.org/library/urllib.html#urllib.urlopen : "a file-like object is returned" then http://docs.python.org/library/stdtypes.html#file.read
RichieHindle
A: 

If you don't mind reading the whole zip-file to memory, the fastest way to read and write it is as follows:

data  = f.readlines()
with open(localFile,'wb') as output:
    output.writelines(data)

Otherwise, to read and write in chunks as you get them over the network, do

with open(localFile, "wb") as output:
    chunk = f.read()
    while chunk:
        output.write(chunk)
        chunk = f.read()

This is a little less neat, but avoids keeping the whole file in memory at once. Hope it helps.

James
A: 

Try this:

#download file
f = o.open(remoteFileUrl)

response = ""
while 1:
    data = f.read()
    if not data:
        break
    response += data

with open(localFile, "wb") as local_file:
    local_file.write(response)
leoluk