From: Can Sarıgöl Date: Tue, 6 Aug 2019 13:31:04 +0000 (+0300) Subject: Check `~/.netrc` and `~/_netrc` files by default (#189) X-Git-Tag: 0.7.0~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=625ad127cc9f5b8338481f8fc2b0b851bac2324f;p=thirdparty%2Fhttpx.git Check `~/.netrc` and `~/_netrc` files by default (#189) --- diff --git a/httpx/utils.py b/httpx/utils.py index 4831578d..61493a6a 100644 --- a/httpx/utils.py +++ b/httpx/utils.py @@ -83,10 +83,24 @@ def guess_json_utf(data: bytes) -> typing.Optional[str]: return None +NETRC_STATIC_FILES = tuple( + os.path.expanduser(path) for path in ("~/.netrc", "~/_netrc") +) + + def get_netrc_login(host: str) -> typing.Optional[typing.Tuple[str, str, str]]: - try: - netrc_info = netrc.netrc(os.environ.get("NETRC")) # type: ignore - except FileNotFoundError: + NETRC_FILES = ( + (os.environ["NETRC"],) if "NETRC" in os.environ else NETRC_STATIC_FILES + ) + netrc_path = None + + for file_path in NETRC_FILES: + if os.path.isfile(file_path): + netrc_path = file_path + break + + if netrc_path is None: return None + netrc_info = netrc.netrc(netrc_path) return netrc_info.authenticators(host) # type: ignore