How to attach .netrc file to python requests get method?
Asked Answered
B

2

6

I am trying to login to a site using python requests. Normally I do this using curl in the cmd.

curl -c <path/urs_cookies.txt> -n -L https://site_FileUpload/login

Where the -n searches for the .netrc file in the cwd which contains my username and password.

However doing this in python using does not work

login = requests.get(url, auth=(username, password)) 

I believe this is because I need to use my .netrc file instead of using the authorisation method.

Is there anyway I can attach this file to my login request?

Thanks a lot

Brachio answered 25/2, 2021 at 14:19 Comment(0)
B
9

If your .netrc file exists in your home directory, python requests will automatically check that file for credentials under a given machine (hostname) when the auth argument is not provided.

From the requests documentation: https://requests.readthedocs.io/en/latest/user/authentication/?highlight=.netrc#netrc-authentication

If no authentication method is given with the auth argument, Requests will attempt to get the authentication credentials for the URL’s hostname from the user’s netrc file. The netrc file overrides raw HTTP authentication headers set with headers=.

If credentials for the hostname are found, the request is sent with HTTP Basic Auth.

The following should work assuming you have a valid .netrc file in your home directory. https://www.labkey.org/Documentation/wiki-page.view?name=netrc

login = requests.get(url)
Balikpapan answered 5/8, 2021 at 22:53 Comment(0)
K
2

You don't need to pass .netrc file while requesting via python request library. Only requirement is that .netrc file should be in your home directory. I used to trigger a databricks job via python which required all the credentials in .netrc file. I simply used requests.post(url, data) and kept my .netrc file in the home directory and it was working.

Kareykari answered 7/9, 2021 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.