繁体   English   中英

Python - 使用进度条和基本身份验证通过HTTP下载文件

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

我正在使用urllib.urlretrieve下载文件,并使用reporthook参数实现下载进度条。 由于urlretrieve不直接支持身份验证,我想出了

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)

这有效 - 但似乎可能有更直接的方法来做到这一点(可能使用urllib2或httplib2或......) - 任何想法?

urlgrabber内置了对进度条,身份验证等的支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM