gpt4 book ai didi

python - urllib2.HTTPError : HTTP Error 401: Unauthorized

转载 作者:太空狗 更新时间:2023-10-30 01:38:16 28 4
gpt4 key购买 nike

我的要求是从某个网站下载一个 abc.zip 文件 http://clientdownload.xyz.com/Documents/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

我已经正确地提供了用户名和密码,但它引发了未经授权的错误。

当我尝试使用带有 -http-user 和 --ask-password 选项的 wget 链接下载它时,我能够下载该文件。

我还可以使用相同的脚本从其他服务器正确下载文件。

我运行此脚本以获取更多信息:

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

最佳答案

您可以考虑使用 requests使通过 HTTP 进行交互更容易。在您的情况下,通过安装 requests-ntlm (请求的插件)你会得到NTLM authentication以更透明的方式:

import requests
from requests_ntlm import HttpNtlmAuth

r = requests.get("http://ntlm_protected_site.com",auth=HttpNtlmAuth('domain\\username','password'))

r 保存响应,包括 error codesheaders (专门针对您的案例 r.headers.get('Content-Length')[0])

关于python - urllib2.HTTPError : HTTP Error 401: Unauthorized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21729255/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com