Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Requests: follow a link

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?

like image 488
Mark Kennedy Avatar asked Apr 26 '26 04:04

Mark Kennedy


1 Answers

"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
like image 82
clj Avatar answered Apr 28 '26 16:04

clj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!