|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Downloading a zip file
I'm trying to downlod a .zip file over HTTP and save it to the local filesystem (then extract it).
I'm doing: Code:
conn = httplib.HTTPConnection( "www.myserver.com" )
conn.request( "GET", "/images/devnews_images.zip" )
resp = conn.getresponse()
if resp.status != 200 and resp.status != 302:
print "Could not download images. Proceeding without."
return
fobj = open( dest+"/devnews_images.zip", 'w' )
fobj.write( resp.read() )
fobj.close()
... the zip file gets created but it's not a valid .zip (although it does appear to be a binary file). It's A little larger than the original zip. Anyone konw if I have to do anything special? I don't see any options for binary mode transfers, but then I thought HTTP was text-only? Thanks, Antun |
|
#2
|
|||
|
|||
|
Never mind! I had to tell the open function that it was a binary file:
Code:
fobj = open(dest+"/devnews_images.zip", 'wb') |
|
#3
|
|||
|
|||
|
you opened the local file (fobj) in text mode, so I am guessing that the file became corrupted when you saved it to disk.
Change it to Code:
fobj = open( dest+"/devnews_images.zip", 'wb' ) and see if that makes any difference. Alternatively use the urllib.urlretrieve function, which will handle everything for you. Dave - The Developers' Coach |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Downloading a zip file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|