#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    40
    Rep Power
    12

    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    40
    Rep Power
    12
    Never mind! I had to tell the open function that it was a binary file:

    Code:
    fobj = open(dest+"/devnews_images.zip", 'wb')
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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

IMN logo majestic logo threadwatch logo seochat tools logo