To use auto-detection you need to set the default_encoding argument to a callable instead of a string. In cases where the server is not reliably including character set information, and where we don't know what encoding is being used, we can enable auto-detection to make a best-guess attempt when decoding from bytes to text. text ) # The text will either be decoded with the Content-Type # charset, or using "shift-jis". encoding ) # This will either print the charset given in # the Content-Type charset, or else "shift-jis". Client ( default_encoding = "shift-jis" ) # Using the client. Import httpx # Instantiate a client with a Japanese character set as the default encoding. This will ensure that connections are properly cleaned up when leaving the with block: The recommended way to use a Client is as a context manager. The other sections on this page go into further detail about what you can do with a Client instance. Applying configuration across all outgoing requests.Reduced latency across requests (no handshaking).Ĭlient instances also support features that aren't available at the top-level API, such as:. This can bring significant performance improvements compared to using the top-level API, including: This means that when you make several requests to the same host, the Client will reuse the underlying TCP connection, instead of recreating one for every single request. On the other hand, a Client instance uses HTTP connection pooling. As the number of requests to a host increases, this quickly becomes inefficient. When you make requests using the top-level API as documented in the Quickstart guide, HTTPX has to establish a new connection for every single request (connections are not reused). More efficient usage of network resources If you do anything more than experimentation, one-off scripts, or prototypes, then you should use a Client instance. More efficient usage of network resourcesĬharacter set encodings and auto-detection
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |