2013-09-02 8 views
2

인트라넷에서 사이트를 구문 분석하려고하는데 아래에서 인증 할 때 인증이 필요하다는 오류가 나타납니다. 왜이 401 오류가 계속 발생합니까?Python이 ntlm을 사용하여 인증 할 수 없습니다.

미리 감사드립니다.

File "C:\Python27\lib\urllib2.py", line 531, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 401: Authorization Required

import urllib2 
from ntlm import HTTPNtlmAuthHandler 

user = r'domain\myuser' 
password = 'mypasswd' 
url = 'http://myinternal.homepage' 

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) 
print(response) 
+0

최신 http://code.google.com/p/python-ntlm/ 및 http://stackoverflow.com/a/14904367/819417을 사용해보십시오. –

답변

1

은 '도메인 \ 참고 MyUser'앞에 'R'을 putthing하지보십시오. 나는 이것을 'r'없이 사용했고 그것은 나를 위해 일한다. 나에게 도움이 된 한가지 - (아마도 당신이 이미 생각한 것 같은데 ...) URL이 당신에게 반환한다는 헤더를 확인하십시오. 나는 Mechanize http://www.pythonforbeginners.com/cheatsheet/python-mechanize-cheat-sheet/으로 이것을 수행했고 NTLM auth를 사용해야한다고 계산 된 헤더를 기반으로 여기에 표시되었습니다. 비슷한 질문이 있습니다 How to 'convert' variable of type instance such that the variable can be used to authenticate when making system calls.

관련 문제