18

I am using python request library to access the soap requests. And it was working fine. As there is change in our domain structure. I could not access the url, it always prompting me to enter the credentials.

enter image description here

I am using below code to access the url earlier using requests.

program_list_response = requests.get(program_list_path,
                                                 data=self.body, headers=self.headers)

How to pass the authentication in background using requests?

1 Answer 1

38

You can use the Authentication feature for that in order to provide the credentials for the link that you want to access.

For an eg:

You can pass the username and password by using the below format:

requests.get('https://website.com/user', auth=('user', 'pass'))

For more details I would recommend the official docs.

For handling the Windows authentication then I would recommend the Requests-NTLM.

For eg:

import requests
from requests_ntlm import HttpNtlmAuth

requests.get("http://ntlm_protected_site.com",auth=HttpNtlmAuth('domain\\username','password'))
Sign up to request clarification or add additional context in comments.

4 Comments

Keep in mind all the different authentication methods, e.g. although the device I was trying to access was basic and the interface looked like it, I needed the auth=HTTPDigestAuth('user', 'pass')
For future people looking at this, I've spent a while looking around at different options and the HttpNtlmAuth solution was the only one that worked for me.
To expand on this topic, if you are already logged into a domain, and don't want to keep the username and password stored in your script, use requests-negotiate-sspi instead of requests_ntlm
@rayzinnz Excellent suggestion - this was exactly what I was searching for. It's actually a PyPi project, as well, and can be installed via pip: pypi.org/project/requests-negotiate-sspi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.