Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - downloading a file over HTTP with progress bar and basic authentication

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?

like image 467
dF. Avatar asked Jan 15 '09 19:01

dF.


1 Answers

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

like image 111
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 05:09

Ignacio Vazquez-Abrams