views:

1255

answers:

1

I'm using urllib.urlretrieve to download a file, and implementing a download progress bar using the reporthook parameter. Since urlretrieve doesn't directly support authentication, I came up with

import urllib

def urlretrieve_with_basic_auth(url, filename=None, reporthook=None, data=None,
                                username="", password=""):
    class OpenerWithAuth(urllib.FancyURLopener):
        def prompt_user_passwd(self, host, realm):
            return username, password

    return OpenerWithAuth().retrieve(url, filename, reporthook, data)

This works -- but it seems like there might be a more direct way to do this (maybe with urllib2 or httplib2 or...) --any ideas?

+5  A: 

urlgrabber has built-in support for progress bars, authentication, and more.

Ignacio Vazquez-Abrams
Nice, will work perfectly for my current project
Baresi
Ignacio's URI is now (2010-06-25) out of date, update one is [urlgrabber](http://linux.duke.edu/urlgrabber/)
kappa