【发布时间】:2009-01-15 19:52:18
【问题描述】:
我正在使用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 或...)——有什么想法吗?
【问题讨论】: