2 # Copyright 2009, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Download a file from a URL to a file on disk.
33 This module supports username and password with basic authentication.
41 def _CreateDirectory(path
):
42 """Create a directory tree, ignore if it's already there."""
50 def HttpDownload(url
, target
, username
=None, password
=None):
51 """Download a file from a remote server.
54 url: A URL to download from.
55 target: Filename to write download to.
56 username: Optional username for download.
57 password: Optional password for download (ignored if no username).
60 headers
= [('Accept', '*/*')]
63 auth_code
= base64
.b64encode(username
+ ':' + password
)
65 auth_code
= base64
.b64encode(username
)
66 headers
.append(('Authorization', 'Basic ' + auth_code
))
67 opener
= urllib2
.build_opener()
68 opener
.addheaders
= headers
69 urllib2
.install_opener(opener
)
70 src
= urllib2
.urlopen(url
)
74 _CreateDirectory(os
.path
.split(target
)[0])
75 fh
= open(target
, 'wb')