Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 25th, 2004, 04:16 PM
karlovac karlovac is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 40 karlovac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #2  
Old May 25th, 2004, 04:34 PM
karlovac karlovac is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 40 karlovac User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Never mind! I had to tell the open function that it was a binary file:

Code:
fobj = open(dest+"/devnews_images.zip", 'wb')

Reply With Quote
  #3  
Old May 25th, 2004, 06:00 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,224 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 23 h 4 m 39 sec
Reputation Power: 263
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Downloading a zip file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT