▼ Click the card below to follow me
▲ Click the card above to follow me
Httpx, an asynchronous HTTP client, is a powerful tool for Python! When it comes to making network requests, it truly excels. Think about it, previously when you used the requests
library, you had to wait for one request to complete before sending the next. Now with Httpx, requests can be sent out like wild horses, concurrently, efficiently! That’s the gist of it, let’s dive deeper.
Installing and Importing Httpx
Installing Httpx is as simple as running a pip command:
pip install httpx
Using it is also straightforward:
import httpx
Testing a GET Request
The most commonly used GET request is a piece of cake with Httpx:
async def fetch_data(url): async with httpx.AsyncClient() as client: # Establish a connection, automatically closed after use, worry-free! response = await client.get(url) # Note the await here, it's asynchronous if response.status_code == 200: # 200 means the request was successful return response.json() # Usually returns JSON data, parsed directly else: print(f"Request failed! Status code: {response.status_code}") # If there's an error, print the status code to check return None # No data retrieved, return None
# Now, let's run it
import asyncio
asyncio.run(fetch_data("https://www.example.com"))
Friendly reminder: Don’t forget async
and await
, they are essential for asynchronous programming; you can’t do without them.
POST Request with Data
A POST request usually requires sending some data, like submitting a form. Httpx can handle that easily:
async def post_data(url, data): async with httpx.AsyncClient() as client: response = await client.post(url, data=data) # The subsequent handling is similar to GET requests, so I won't elaborate # ...
The data
parameter is the data you want to send, formatted as a dictionary.
Timeout Settings to Avoid Hanging
When making network requests, it’s inevitable to encounter poor network conditions that may cause requests to hang. Setting a timeout can prevent the program from waiting indefinitely:
async def fetch_with_timeout(url): async with httpx.AsyncClient(timeout=10.0) as client: # 10 seconds timeout, quite generous # ... The following code is similar to the previous ones
A 10-second timeout means that if there is no response after 10 seconds, an exception will be thrown, allowing the program to continue without hanging.
Handling Non-JSON Data
Sometimes the server returns data that is not in JSON format, such as images or files. Httpx can handle that too:
async def fetch_image(url): async with httpx.AsyncClient() as client: response = await client.get(url) if response.status_code == 200: return response.content # Directly retrieve the raw data # ...
The response.content
retrieves the byte stream, which you can process as you wish.
Streaming Requests for Large Files
When downloading large files, using streaming requests allows you to process the data as it downloads, which is memory-friendly:
async def stream_download(url, filename): async with httpx.AsyncClient() as client: async with client.stream("GET", url) as response: # Note that this is the stream method with open(filename, "wb") as file: # Open the file in binary write mode async for chunk in response.aiter_bytes(): # Asynchronously iterate, writing chunk by chunk file.write(chunk)# ...
This method is suitable for downloading large files, avoiding loading the entire file into memory, which could cause memory overflow.
Httpx, the asynchronous HTTP client, is powerful and user-friendly. When it comes to making network requests, it is definitely a great helper!
Like and Share
Let money and love flow to you