Skip to content

Commit

Permalink
Respect the NETRC environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mpichette-apple committed Nov 1, 2020
1 parent 1431502 commit f15769d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/user/quickstart.rst
Expand Up @@ -213,7 +213,8 @@ Note: Custom headers are given less precedence than more specific sources of inf

* Authorization headers set with `headers=` will be overridden if credentials
are specified in ``.netrc``, which in turn will be overridden by the ``auth=``
parameter.
parameter. Requests will search for the netrc file at `~/.netrc`, `~/_netrc`,
or at the path specified by the `NETRC` environment variable.
* Authorization headers will be removed if you get redirected off-host.
* Proxy-Authorization headers will be overridden by proxy credentials provided in the URL.
* Content-Length headers will be overridden when we can determine the length of the content.
Expand Down
9 changes: 6 additions & 3 deletions requests/utils.py
Expand Up @@ -35,7 +35,7 @@
from .exceptions import (
InvalidURL, InvalidHeader, FileModeWarning, UnrewindableBodyError)

NETRC_FILES = ('.netrc', '_netrc')
NETRC_FILES = ('~/.netrc', '~/_netrc')

DEFAULT_CA_BUNDLE_PATH = certs.where()

Expand Down Expand Up @@ -169,14 +169,17 @@ 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')
netrc_locations = (netrc_file,) if netrc_file is not None else 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 &
Expand Down

0 comments on commit f15769d

Please sign in to comment.