diff options
author | Nate Prewitt <nate.prewitt@gmail.com> | 2020-11-01 08:52:01 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-01 08:52:01 -0800 |
commit | 2f70990cd3fabf7b05cb9a69b3dab1a43bbf0096 (patch) | |
tree | 967db6fa3e2e5e8c61600aaaf2e4226a6eac2daa /requests/utils.py | |
parent | 143150233162d609330941ec2aacde5ed4caa510 (diff) | |
parent | ba543713d35067866d68b09f644042c0c021a8ba (diff) | |
download | python-requests-2f70990cd3fabf7b05cb9a69b3dab1a43bbf0096.tar.gz |
Merge pull request #5643 from tarmath/netrc
Respect the NETRC environment variable
Diffstat (limited to 'requests/utils.py')
-rw-r--r-- | requests/utils.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/requests/utils.py b/requests/utils.py index 1aafd9cb..16d57762 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -169,14 +169,20 @@ def super_len(o): def get_netrc_auth(url, raise_errors=False): """Returns the Requests tuple auth for a given url from netrc.""" + netrc_file = os.environ.get('NETRC') + if netrc_file is not None: + netrc_locations = (netrc_file,) + else: + netrc_locations = ('~/{}'.format(f) for f in NETRC_FILES) + try: from netrc import netrc, NetrcParseError netrc_path = None - for f in NETRC_FILES: + for f in netrc_locations: try: - loc = os.path.expanduser('~/{}'.format(f)) + loc = os.path.expanduser(f) except KeyError: # os.path.expanduser can fail when $HOME is undefined and # getpwuid fails. See https://bugs.python.org/issue20164 & |