【问题标题】:IndexError using python-ntlm使用 python-ntlm 的 IndexError
【发布时间】:2011-10-31 11:07:23
【问题描述】:

我正在尝试使用urllib2python-ntlm 连接到经过NT 身份验证的服务器,但出现错误。这是我正在使用的代码,来自the python-ntlm site

user = 'DOMAIN\user.name'
password = 'Password123'
url = 'http://corporate.domain.com/page.aspx?id=foobar'

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, user, password)
# create the NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)

# create and install the opener
opener = urllib2.build_opener(auth_NTLM)
urllib2.install_opener(opener)

# retrieve the result
response = urllib2.urlopen(url)
return response.read()

这是我得到的错误:

Traceback (most recent call last):
  File "C:\Python27\test.py", line 112, in get_ntlm_data
    response = urllib2.urlopen(url)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 398, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 511, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 430, in error
    result = self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 370, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\site-packages\python_ntlm-1.0.1-py2.7.egg\ntlm\HTTPNtlmAuthHandler.py", line 99, in http_error_401
    return self.http_error_authentication_required('www-authenticate', req, fp, headers)
  File "C:\Python27\lib\site-packages\python_ntlm-1.0.1-py2.7.egg\ntlm\HTTPNtlmAuthHandler.py", line 35, in http_error_authentication_required
    return self.retry_using_http_NTLM_auth(req, auth_header_field, None, headers)
  File "C:\Python27\lib\site-packages\python_ntlm-1.0.1-py2.7.egg\ntlm\HTTPNtlmAuthHandler.py", line 72, in retry_using_http_NTLM_auth
    UserName = user_parts[1]
IndexError: list index out of range

知道我做错了什么吗?

【问题讨论】:

  • 我最终放弃并改用 curl。
  • 我也有同样的问题,但是使用 suds 和 python_ntlm

标签: python urllib2 ntlm


【解决方案1】:

试试:

user = r'DOMAIN\user.name'

【讨论】:

  • 为了完整性,user = 'DOMAIN\\user.name 也可以。字符串转义是问题所在。我认为应该由@ryan-mcgreal 将答案标记为已接受。
  • 问题是身份验证失败,并且在库中处理不当。很高兴指出这一点,因为其他人在谷歌上搜索症状/错误消息。
猜你喜欢
  • 2018-08-10
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
相关资源
最近更新 更多