HTTPX is a powerful HTTP client library for Python 3 that integrates a command-line client, supports HTTP/1.1 and HTTP/2, and provides both synchronous and asynchronous APIs. This article will detail the features, usage, and packaging process of HTTPX.
1. Core Features of HTTPX
The design goal of HTTPX is to be a modern, fully-featured HTTP client that is optimized for usability, performance, and functionality. Its core features include:
-
• Requests-Compatible API: The API design of HTTPX is highly compatible with the widely used
<span>requests</span>library, making it very easy to migrate from<span>requests</span>to HTTPX, thus reducing the learning curve. -
• Integrated Command-Line Client: HTTPX comes with a convenient command-line tool that allows users to send HTTP requests directly from the command line without writing Python code. This is very useful for quick testing and simple HTTP interactions. To install the command-line client, use
<span>pip install 'httpx[cli]'</span>. -
• Support for HTTP/1.1 and HTTP/2: HTTPX supports both HTTP/1.1 and HTTP/2 protocols, automatically selecting the appropriate protocol based on the server’s capabilities, enhancing performance and efficiency.
-
• Synchronous and Asynchronous APIs: HTTPX provides a standard synchronous interface while also supporting asynchronous programming, allowing developers to choose the appropriate programming model based on their needs and fully leverage the performance benefits of asynchronous programming.
-
• Support for WSGI and ASGI Applications: HTTPX can be used to send requests directly to WSGI and ASGI applications, making it very convenient for testing and integration with other Python web frameworks.
-
• Strict Timeout Settings: HTTPX provides strict timeout control for all operations, preventing requests from blocking for extended periods.
-
• Complete Type Annotations: HTTPX code features complete type annotations, enhancing code readability and maintainability, and facilitating the use of static type checking tools.
-
• 100% Test Coverage: HTTPX boasts extremely high test coverage, ensuring code quality and stability.
-
• Rich Functionality: HTTPX also includes most features of the
<span>requests</span>library, such as international domain name and URL support, Keep-Alive and connection pooling, session and cookie persistence, browser-style SSL verification, basic/digest authentication, elegant key-value pair cookies, automatic decompression, automatic content decoding, Unicode response bodies, multipart file uploads, HTTP(S) proxy support, connection timeouts, streaming downloads, .netrc support, chunked requests, and more.
2. How to Use HTTPX
Using HTTPX is very simple and intuitive. Here are a few common examples:
import httpx
# Send a GET request
with httpx.Client() as client:
response = client.get('https://www.example.org/')
print(response.status_code) # Print status code
print(response.text) # Print response text
print(response.headers['content-type']) # Print response headers
# Send a POST request
with httpx.Client() as client:
response = client.post('https://httpbin.org/post', data={'key': 'value'})
print(response.json()) # Print JSON response
# Using the asynchronous API
async def fetch_async():
async with httpx.AsyncClient() as client:
response = await client.get('https://www.example.org/')
print(response.status_code)
import asyncio
asyncio.run(fetch_async())
3. Installation and Optional Dependencies for HTTPX
Install HTTPX using pip:
pip install httpx
To include optional HTTP/2 support, use:
pip install httpx[http2]
To install the command-line client:
pip install 'httpx[cli]'
HTTPX requires Python 3.8 or higher.
Conclusion
HTTPX is a powerful, easy-to-use, and efficient Python HTTP client that combines the usability of the<span>requests</span> library with the advanced features of modern HTTP clients. Its support for HTTP/2, synchronous and asynchronous APIs, and rich functionality make it an ideal choice for various Python projects. Whether for simple HTTP requests or complex asynchronous network operations, HTTPX provides reliable and efficient solutions.
Project URL: https://github.com/encode/httpx