Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python脚本中的HTTP基本身份验证失败_Python_Basic Authentication_Urllib - Fatal编程技术网

python脚本中的HTTP基本身份验证失败

python脚本中的HTTP基本身份验证失败,python,basic-authentication,urllib,Python,Basic Authentication,Urllib,我正在尝试连接到REST资源,并使用Python脚本(Python 3.2.3)检索数据。当我运行脚本时,我得到的错误是HTTP错误401:Unauthorized。请注意,我可以使用基本身份验证使用REST客户端访问给定的REST资源。在REST客户机中,我指定了主机名、用户和密码详细信息(不需要领域)。 下面是代码和完整错误。非常感谢你的帮助 代码: import urllib.request # set up authentication info auth_handler = urll

我正在尝试连接到REST资源,并使用Python脚本(Python 3.2.3)检索数据。当我运行脚本时,我得到的错误是HTTP错误401:Unauthorized。请注意,我可以使用基本身份验证使用REST客户端访问给定的REST资源。在REST客户机中,我指定了主机名、用户和密码详细信息(不需要领域)。 下面是代码和完整错误。非常感谢你的帮助

代码:

import urllib.request

# set up authentication info
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm=None,
                       uri=r'http://hostname/',
                       user='administrator',
                       passwd='administrator')
opener =  urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
res = opener.open(r'http://hostname:9004/apollo-api/nodes')
nodes = res.read()
错误

Traceback (most recent call last):
File "C:\Python32\scripts\get-nodes.py", line 12, in <module>
    res = opener.open(r'http://tolowa.wysdm.lab.emc.com:9004/apollo-api/nodes')
File "C:\Python32\lib\urllib\request.py", line 375, in open
   response = meth(req, response)
File "C:\Python32\lib\urllib\request.py", line 487, in http_response
   'http', request, response, code, msg, hdrs)
File "C:\Python32\lib\urllib\request.py", line 413, in error
   return self._call_chain(*args)
File "C:\Python32\lib\urllib\request.py", line 347, in _call_chain
   result = func(*args)
File "C:\Python32\lib\urllib\request.py", line 495, in http_error_default
   raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized
回溯(最近一次呼叫最后一次):
文件“C:\Python32\scripts\get nodes.py”,第12行,在
res=开启器。开启(r'http://tolowa.wysdm.lab.emc.com:9004/apollo-api/节点“)
文件“C:\Python32\lib\urllib\request.py”,第375行,打开
响应=方法(请求,响应)
文件“C:\Python32\lib\urllib\request.py”,第487行,在http\U响应中
“http”、请求、响应、代码、消息、hdrs)
文件“C:\Python32\lib\urllib\request.py”第413行出错
返回自我。调用链(*args)
文件“C:\Python32\lib\urllib\request.py”,第347行,在调用链中
结果=func(*args)
文件“C:\Python32\lib\urllib\request.py”,第495行,默认为http\u error\u
raise HTTPError(请求完整的url、代码、消息、hdrs、fp)
urllib.error.HTTPError:HTTP错误401:未经授权

尝试给出正确的域名。例如,当在浏览器中打开页面时,您可以发现这一点-密码提示应显示名称。

您还可以通过捕获引发的异常来读取域:

import urllib.error
import urllib.request

# set up authentication info
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm=None,
                       uri=r'http://hostname/',
                       user='administrator',
                       passwd='administrator')
opener =  urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
try:
    res = opener.open(r'http://hostname:9004/apollo-api/nodes')
    nodes = res.read()
except urllib.error.HTTPError as e:
    print(e.headers['www-authenticate'])
您应该获得以下输出:

Basic realm="The realm you are after"

从上面阅读领域,并将其设置在您的
add_password
方法中,这样做应该很好。

这样做,或者使用
HTTPPasswordMgrWithDefaultRealm
密码管理器。感谢jOnes和@Martijn Pieters帮助我解决此问题。python中的许多内置http客户端都很麻烦;您是否考虑过使用其他客户,例如:感谢@Thierry Lam帮助我解决此问题。