网页 综合 更新时间 2026-11-14 16:32:27

Authentication - HTTPX

自动蜘蛛 www.python-httpx.org 正文约 3146 字
Authentication can either be included on a per-request basis...
Or configured on the client instance, ensuring that all outgoing requests will include authentication credentials...
HTTP basic authentication is an unencrypted authentication scheme that uses a simple encoding of the username and password in the request Authorization header. Since it is unencrypted it should typically only be used over https, although this is not strictly enforced.
HTTP digest authentication is a challenge-response authentication scheme. Unlike basic authentication it provides encryption, and can be used over unencrypted http connections. It requires an additional round-trip in order to negotiate the authentication.
HTTPX can be configured to use a .netrc config file for authentication.
The .netrc config file allows authentication credentials to be associated with specified hosts. When a request is made to a host that is found in the netrc file, the username and password will be included using HTTP basic authentication.
Example .netrc file:
Some examples of configuring .netrc authentication with httpx.
Use the default .netrc file in the users home directory:
Use an explicit path to a .netrc file:
Use the NETRC environment variable to configure a path to the .netrc file, or fallback to the default.
The NetRCAuth() class uses the netrc.netrc() function from the Python standard library. See the documentation there for more details on exceptions that may be raised if the .netrc file is not found, or cannot be parsed.
When issuing requests or instantiating a client, the auth argument can be used to pass an authentication scheme to use. The auth argument may be one of the following...
The most involved of these is the last, which allows you to create authentication flows involving one or more requests. A subclass of httpx.Auth should implement def auth_flow(request), and yield any requests that need to be made...
If the auth flow requires more than one request, you can issue multiple yields, and obtain the response in each case...
Custom authentication classes are designed to not perform any I/O, so that they may be used with both sync and async client instances. If you are implementing an authentication scheme that requires the request body, then you need to indicate this on the class using a requires_request_body property.
You will then be able to access request.content inside the .auth_flow() method.
Similarly, if you are implementing a scheme that requires access to the response body, then use the requires_response_body property. You will then be able to access response body properties and methods such as response.content, response.text, response.json(), etc.
If you do need to perform I/O other than HTTP requests, such as accessing a disk-based cache, or you need to use concurrency primitives, such as locks, then you should override .sync_auth_flow() and .async_auth_flow() (instead of .auth_flow()). The former will be used by httpx.Client, while the latter will be used by httpx.AsyncClient.
If you only want to support one of the two methods, then you should still override it, but raise an explicit RuntimeError.
访问原文 返回搜索 https://www.python-httpx.org/advanced/authentication/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。