HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is
not defined, HTTPX tries to add auth into request's header from .netrc file.
+!!! note
+ The NETRC file is cached across requests made by a client.
+ If you need to refresh the cache (e.g. because the NETRC file has changed),
+ you should create a new client or restart the interpreter.
+
As default `trust_env` is true. To set false:
```python
>>> httpx.get('https://example.org/', trust_env=False)
import functools
import inspect
+import netrc
import typing
from types import TracebackType
ResponseContent,
URLTypes,
)
-from .utils import ElapsedTimer, get_environment_proxies, get_netrc_login
+from .utils import ElapsedTimer, get_environment_proxies, get_netrc
class BaseClient:
)
if trust_env:
- netrc_login = get_netrc_login(request.url.authority)
- if netrc_login:
- username, _, password = netrc_login
- return BasicAuthMiddleware(username=username, password=password)
+ netrc_info = self._get_netrc()
+ if netrc_info:
+ netrc_login = netrc_info.authenticators(request.url.authority)
+ if netrc_login:
+ username, _, password = netrc_login
+ assert password is not None
+ return BasicAuthMiddleware(username=username, password=password)
return None
+ @functools.lru_cache(1)
+ def _get_netrc(self) -> typing.Optional[netrc.netrc]:
+ return get_netrc()
+
def _dispatcher_for_request(
self, request: AsyncRequest, proxies: typing.Dict[str, AsyncDispatcher]
) -> AsyncDispatcher:
NETRC_STATIC_FILES = (Path("~/.netrc"), Path("~/_netrc"))
-def get_netrc_login(host: str) -> typing.Optional[typing.Tuple[str, str, str]]:
+def get_netrc() -> typing.Optional[netrc.netrc]:
NETRC_FILES = (Path(os.getenv("NETRC", "")),) + NETRC_STATIC_FILES
netrc_path = None
if netrc_path is None:
return None
-
- netrc_info = netrc.netrc(str(netrc_path))
- return netrc_info.authenticators(host) # type: ignore
+ return netrc.netrc(str(netrc_path))
def get_ca_bundle_from_env() -> typing.Optional[str]:
ElapsedTimer,
get_ca_bundle_from_env,
get_environment_proxies,
- get_netrc_login,
+ get_netrc,
guess_json_utf,
obfuscate_sensitive_headers,
parse_header_links,
def test_bad_get_netrc_login():
- assert get_netrc_login("url") is None
-
os.environ["NETRC"] = "tests/.netrc"
- assert get_netrc_login("url") is None
-
- os.environ["NETRC"] = "wrongpath"
- assert get_netrc_login("url") is None
+ assert str(get_netrc()) is not None
from httpx import utils
utils.NETRC_STATIC_FILES = ()
+
+ os.environ["NETRC"] = "wrongpath"
+ assert utils.get_netrc() is None
+
os.environ["NETRC"] = ""
- assert utils.get_netrc_login("url") is None
+ assert utils.get_netrc() is None
def test_get_netrc_login():
os.environ["NETRC"] = "tests/.netrc"
- assert get_netrc_login("netrcexample.org") == (
+ netrc = get_netrc()
+ assert netrc.authenticators("netrcexample.org") == (
"example-username",
None,
"example-password",