2011-10-31 76 views
1

我試圖使用urllib2python-ntlm連接到NT認證的服務器,但出現錯誤。下面是我使用的代碼,從the python-ntlm siteIndexError using python-ntlm

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 

任何想法,我做錯了嗎?

+0

我最終放棄並使用curl來代替。 –

+0

我有這個相同的問題,但使用泡沫python_ntlm – Rich

回答

5

嘗試:

user = r'DOMAIN\user.name' 
+0

這對我有效!謝謝。 – Rich

+0

這也適用於我! 。@ boubou,你能否將此標記爲已接受。 – g4ur4v

+0

只是爲了完整性,'user ='DOMAIN \\ user.name'也可以。字符串轉義是什麼問題。我認爲應該是@ ryan-mcgreal誰應該將答案標記爲已接受。 – Steen