httpx: A Modern HTTP Client Library for Python!

httpx: A Modern HTTP Client Library for Python!

Hello everyone, today I want to share a particularly useful Python library – httpx. Are you still using requests to send HTTP requests? Let me introduce this more modern and powerful new option! httpx not only supports the latest HTTP/2 protocol but also handles asynchronous requests, making it a great tool for web scraping and API development.

Why Choose httpx?

As a Python developer, I was initially a loyal user of requests. However, when I discovered httpx, I was deeply attracted by its features:

  • Support for both synchronous and asynchronous requests
  • Full HTTP/2 support
  • Automatic redirection handling
  • Built-in timeout protection
  • Reliable connection pool management

Quick Start: Installation and Basic Usage

We need to install httpx:

pip install httpx

Let’s take a look at how to write a basic GET request:

import httpx
# Send a GET request
r = httpx.get('https://api.github.com/users/github')
print(r.status_code)  # Check the status code
print(r.json())       # Get JSON response

Tip: The API design of httpx is very similar to requests, so if you have used requests, you will find it easy to get started!

POST Requests and Request Parameters

Let’s see how to send a POST request and pass parameters:

# Send a POST request
data = {'name': 'xxx', 'message': 'Hello httpx!'}
r = httpx.post('https://httpbin.org/post', json=data)
# URL parameters
params = {'q': 'python', 'page': 1}
r = httpx.get('https://api.example.com/search', params=params)

Asynchronous Requests: Unlocking New Speed

One of the most powerful features of httpx is its support for asynchronous requests! Check out this example:

import asyncio
import httpx
async def get_pokemon(name):
    async with httpx.AsyncClient() as client:
        response = await client.get(f'https://pokeapi.co/api/v2/pokemon/{name}')
        return response.json()
async def main():
    # Request information for multiple Pokemon simultaneously
    pokemon_names = ['pikachu', 'charizard', 'bulbasaur']
    async with httpx.AsyncClient() as client:
        tasks = [get_pokemon(name) for name in pokemon_names]
        results = await asyncio.gather(*tasks)
        for name, data in zip(pokemon_names, results):
            print(f"{name}: {data['weight']}kg")
# Run the asynchronous function
asyncio.run(main())

Note: When using the asynchronous client, remember to use the <span>async with</span> statement to manage resources!

HTTP/2 Support: Faster and More Efficient

Enabling HTTP/2 support is super simple:

client = httpx.Client(http2=True)
r = client.get('https://http2.github.io')
print(r.http_version)  # Output: "HTTP/2"

Practical Timeout Settings

Timeout handling is an important aspect of network requests:

# Set timeout (in seconds)
try:
    r = httpx.get('https://api.example.com', timeout=5.0)
except httpx.TimeoutException:
    print("Request timed out!")

Practical Exercise

Let’s try this small exercise:

  1. Create an asynchronous function to get the response time of multiple websites simultaneously
  2. Use the HTTP/2 client to request a website that supports HTTP/2
  3. Try to handle various possible exceptions

Advanced Tips

  • Using <span>httpx.Client()</span> to create a client can reuse connections and improve performance
  • Remember to handle common exceptions: <span>httpx.RequestError</span>, <span>httpx.TimeoutException</span>
  • In large projects, it is recommended to use the asynchronous client to improve concurrency performance

Friends, today’s Python learning journey ends here! Remember to code, and feel free to ask me any questions in the comments. httpx is truly a powerful library, and I hope you enjoy it and use it in your actual projects. Wishing everyone happy learning and continuous improvement in Python!

Like and share

httpx: A Modern HTTP Client Library for Python!

Let money and love flow to you

Leave a Comment