-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support .netrc by trust_env #2584
Changes from 14 commits
9e130f6
b0ab29d
a8e9049
ec9159a
5362cc6
9f8bbc5
cdfbd06
083bb0f
b9522c0
7b36103
ddc0f0b
d52ed24
46ad4e9
f95d5d1
619eeae
131177a
9e7daa7
a246e52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support `.netrc` by `trust_env` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import datetime | ||
import functools | ||
import inspect | ||
import netrc | ||
import os | ||
import re | ||
import time | ||
|
@@ -111,12 +112,40 @@ def strip_auth_from_url(url): | |
return url.with_user(None), auth | ||
|
||
|
||
def netrc_from_env(): | ||
netrc_obj = None | ||
netrc_path = os.environ.get('NETRC') | ||
try: | ||
if netrc_path is not None: | ||
netrc_path = Path(netrc_path) | ||
else: | ||
home_dir = Path.home() | ||
if os.name == 'nt': # pragma: no cover | ||
netrc_path = home_dir.joinpath('_netrc') | ||
else: | ||
netrc_path = home_dir.joinpath('.netrc') | ||
|
||
if netrc_path and netrc_path.is_file(): | ||
try: | ||
netrc_obj = netrc.netrc(str(netrc_path)) | ||
except (netrc.NetrcParseError, IOError) as e: | ||
client_logger.warning(".netrc file parses fail: %s", e) | ||
|
||
if netrc_obj is None: | ||
client_logger.warning("could't find .netrc file") | ||
except RuntimeError as e: # pragma: no cover | ||
""" handle error raised by pathlib """ | ||
client_logger.warning("could't find .netrc file: %s", e) | ||
return netrc_obj | ||
|
||
|
||
ProxyInfo = namedtuple('ProxyInfo', 'proxy proxy_auth') | ||
|
||
|
||
def proxies_from_env(): | ||
proxy_urls = {k: URL(v) for k, v in getproxies().items() | ||
if k in ('http', 'https')} | ||
netrc_obj = netrc_from_env() | ||
stripped = {k: strip_auth_from_url(v) for k, v in proxy_urls.items()} | ||
ret = {} | ||
for proto, val in stripped.items(): | ||
|
@@ -125,6 +154,12 @@ def proxies_from_env(): | |
client_logger.warning( | ||
"HTTPS proxies %s are not supported, ignoring", proxy) | ||
continue | ||
if netrc_obj and auth is None: | ||
auth_from_netrc = netrc_obj.authenticators(proxy.host) | ||
if auth_from_netrc is not None: | ||
*logins, password = auth_from_netrc | ||
auth = BasicAuth(logins[0] if logins[0] else logins[1], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/requests/requests/blob/24092b11d74af0a766d9cc616622f38adb0044b9/requests/utils.py#L203-L207 if netrc_obj and auth is None:
auth_from_netrc = netrc_obj.authenticators(proxy.host)
if auth_from_netrc is not None:
# auth_from_netrc is a (`user`, `account`, `password`) tuple,
# `user` and `account` both can be username,
# if `user` is None, use `account`
*logins, password = auth_from_netrc
auth = BasicAuth(logins[0] if logins[0] else logins[-1],
password) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
password) | ||
ret[proto] = ProxyInfo(proxy, auth) | ||
return ret | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename
IOError
toOSError
-- they are aliases butOSError
is the preferable name.