我的要求是从一些网站http://clientdownload.xyz.com/Documents/abc.zipurllib2.HTTPError:HTTP错误401:未经授权
下载abc.zip文件对于此活动我写了一个python脚本如下:
url_to_check = 'http://clientdownload.xyz.com/Documents/abc.zip'
username = "user"
password = "pwd"
p = urllib2.HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, url_to_check, username, password)
handler = urllib2.HTTPBasicAuthHandler(p)
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
zip_file = urllib2.urlopen(url_to_check).read()
file_name = 'somefile.zip'
meta = zip_file.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
with open(file_name, 'wb') as dwn_file:
dwn_file.write(zip_file.read())
而我收到以下错误,当我运行该脚本:
File "updateCheck.py", line 68, in check_update zip_file = urllib2.urlopen(url_to_check).read() File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 406, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 519, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 444, in error return self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Unauthorized
我已经给了用户名和密码正确,但它抛出未经授权的错误。
当我试图使用wget链接下载它与-http-user and --ask-password
选项时,我可以下载该文件。
同样使用相同的脚本,我可以正确地从其他服务器下载文件。
我跑这个脚本,以获得更多信息:
import urllib2, re, time, sys
theurl='http://clientdownload.xxx.com/Documents/Forms/AllItems.aspx'
req = urllib2.Request(theurl)
try:
handle = urllib2.urlopen(req)
except IOError, e:
if hasattr(e, 'code'):
if e.code != 401:
print 'We got another error'
print e.code
else:
print e.headers
print e.headers['www-authenticate']
我得到了以下信息:
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
SPRequestGuid: 939bad00-40b7-49b9-bbbc-99d0267a1004
X-SharePointHealthScore: 0
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
MicrosoftSharePointTeamServices: 14.0.0.6029
Date: Wed, 12 Feb 2014 13:14:19 GMT
Connection: close
Content-Length: 16
NTLM
如果我正确理解,您正在使用具有NTLM身份验证的基本身份验证处理程序。尝试一下[像这样](http://code.google.com/p/python-ntlm/)。 –
是的,已经尝试使用Ntlm Auth处理程序,而我的Python安装程序没有NTLM包,所以我得到了以下错误。 ImportError:无法导入名称HTTPNtlmAuthHandler – user3301805
那么,您可以安装软件包或使用[虚拟环境](http://www.virtualenv.org/en/ latestst /)。 virtualenv是Python最佳实践(afaik)的一部分,并允许您安装自定义的东西而不会搞乱你原来的python安装。 –