문제

Suppose I have the following script:

import requests

username = 'myUser'
password = 'myPswd'
url = 'https://NTLMwebsite/base.com'
nextPage = 'https://NTLMwebsite/base/next.com'

r = requests.get(url, auth=HttpNtlmAuth(username,password))
#Cool, but how do I access nextPage?

How do I request another page once I've passed NTLM? authentication. If I simply do another request via: requests.get(nextPage, auth=HttpNtlmAuth(username,password)), then it redirects me back to the base website since it creates a NEW request and has to re-certify NTLM authentication.

Does anyone know how to follow a link ONCE you've bypassed NTLM?

도움이 되었습니까?

해결책

"NTLM authenticates the connection, not single requests", according to the requests_ntlm package's source code. This means that you either have to reuse the same request or keep authenticating with each request you make.

You should be able to use requests' Session object to get a persistent connection that you can reuse, as well as making it easier to set the authentication. The following code demonstrates what is going on and should provide something for you to build on:

import requests
from requests_ntlm import HttpNtlmAuth

username = 'myUser'
password = 'myPswd'
url = 'https://NTLMwebsite/base.com'
nextPage = 'https://NTLMwebsite/base/next.com'

s = requests.Session()

s.auth = HttpNtlmAuth(username, password)

r1 = s.get(url)
r2 = s.get(nextPage)
print 'r1', r1
print 'r2', r2
print 'Session using same connection:', r1.connection == r2.connection

r1 = requests.get(url, auth=HTTPBasicAuth(username, password))
r2 = requests.get(nextPage, auth=HTTPBasicAuth(username, password))
print 'r1', r1
print 'r2', r2
print 'Non Session using same connection:', r1.connection == r2.connection
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top