Skip to content

Commit 2f70990

Browse files
authored
Merge pull request #5643 from tarmath/netrc
Respect the NETRC environment variable
2 parents 1431502 + ba54371 commit 2f70990

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

docs/user/quickstart.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ Note: Custom headers are given less precedence than more specific sources of inf
213213

214214
* Authorization headers set with `headers=` will be overridden if credentials
215215
are specified in ``.netrc``, which in turn will be overridden by the ``auth=``
216-
parameter.
216+
parameter. Requests will search for the netrc file at `~/.netrc`, `~/_netrc`,
217+
or at the path specified by the `NETRC` environment variable.
217218
* Authorization headers will be removed if you get redirected off-host.
218219
* Proxy-Authorization headers will be overridden by proxy credentials provided in the URL.
219220
* Content-Length headers will be overridden when we can determine the length of the content.

requests/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,20 @@ def super_len(o):
169169
def get_netrc_auth(url, raise_errors=False):
170170
"""Returns the Requests tuple auth for a given url from netrc."""
171171

172+
netrc_file = os.environ.get('NETRC')
173+
if netrc_file is not None:
174+
netrc_locations = (netrc_file,)
175+
else:
176+
netrc_locations = ('~/{}'.format(f) for f in NETRC_FILES)
177+
172178
try:
173179
from netrc import netrc, NetrcParseError
174180

175181
netrc_path = None
176182

177-
for f in NETRC_FILES:
183+
for f in netrc_locations:
178184
try:
179-
loc = os.path.expanduser('~/{}'.format(f))
185+
loc = os.path.expanduser(f)
180186
except KeyError:
181187
# os.path.expanduser can fail when $HOME is undefined and
182188
# getpwuid fails. See https://bugs.python.org/issue20164 &

0 commit comments

Comments
 (0)