user1106278
user1106278

Reputation: 767

How do I save a file using python's httplib2?

I can run the following code:

import httplib2

h = httplib2.Http('.cache')

response, content = h.request('http://2.bp.blogspot.com/-CXFfl9luHPM/TV-Os6opQfI/AAAAAAAAA2E/oCgrgvWqzrY/s1600/cow.jpg')

print(response.status)

with open('cow.jpg', 'wb') as f:
    f.write(content)

When I run the code, I download a file called cow.jpg which is what I want, but I also get a duplicate image with a different name called: 2.bp.blogspot.com,-CXFfl9luHPM,TV-Os6opQfI,AAAAAAAAA2E,oCgrgvWqzrY,s1600,cow.jpg,77ba31012a25509bfdc78bea4e1bfdd1. It's the http address with commas plus other junk. Any ideas on how I can create only one image using httplib2? Thanks.

Upvotes: 2

Views: 5815

Answers (3)

Fred
Fred

Reputation: 1021

Use urllib and method urlretrieve, the second argument is the file location.

for python 2.x

import urllib
urllib.urlretrieve(URL, path_destination)

Upvotes: 1

Zach Kelling
Zach Kelling

Reputation: 53859

Just write the content to a file:

with open('cow.jpg', 'wb') as f:
    f.write(content)

Upvotes: 3

ThiefMaster
ThiefMaster

Reputation: 318578

Is using urllib2 ok for you, too? If yes, you can use this function:

def download_file(url):
    """Create an urllib2 request and return the request plus some useful info"""
    name = filename_from_url(url)
    r = urllib2.urlopen(urllib2.Request(url))
    info = r.info()
    if 'Content-Disposition' in info:
        # If the response has Content-Disposition, we take filename from it
        name = info['Content-Disposition'].split('filename=')[1]
        if name[0] == '"' or name[0] == "'":
            name = name[1:-1]
    elif r.geturl() != url:
        # if we were redirected, take the filename from the final url
        name = filename_from_url(r.geturl())
    content_type = None
    if 'Content-Type' in info:
        content_type = info['Content-Type'].split(';')[0]
    # Try to guess missing info
    if not name and not content_type:
        name = 'unknown'
    elif not name:
        name = 'unknown' + mimetypes.guess_extension(content_type) or ''
    elif not content_type:
        content_type = mimetypes.guess_type(name)[0]
    return r, name, content_type

Usage:

fp, filename, content_type = download_file('http://url/to/some/file')
with open('somefile', 'w') as dst:
    shutil.copyfileobj(fp, dst)

This code has the advantage that is never reads the whole file into memory - so it works fine for huge files, too. Besides that, it also gives you the filename received from the server and the content-type in case you want/need it.

Upvotes: 0

Related Questions