▼ Click the card below to follow me
▲ Click the card above to follow me
Httpx, a powerful tool for network requests!
Today, let’s talk about Httpx. It is a Python library specifically designed for making HTTP requests, and it is more powerful than the requests library, supporting both asynchronous and synchronous operations! Remember the days when using requests required a lot of work for asynchronous calls? Now with Httpx, it’s incredibly smooth!
Synchronous Requests, Steady!
First, let’s discuss synchronous requests, which are quite similar to requests, simple and straightforward:
import httpx
response = httpx.get('https://www.example.com')
print(response.text) # The webpage content comes out smoothly
print(response.status_code) # Status code, 200 means success
As you can see, just two lines of code and the webpage is fetched. It’s just as quick to get started as with requests.
Asynchronous Requests, a Flying Experience!
Now for the main event, asynchronous requests! This is where Httpx truly shines. What does asynchronous mean? It means you can send the next request without waiting for the previous one to finish, making it highly efficient!
import httpx
import asyncio
async def fetch(url):
async with httpx.AsyncClient() as client:
response = await client.get(url)
return response.text
async def main():
urls = [
'https://www.example.com',
'https://www.google.com',
'https://www.baidu.com'
]
tasks = [fetch(url) for url in urls]
results = await asyncio.gather(*tasks)
print(results) # Fetches the content of three webpages at once!
asyncio.run(main())
Here we use AsyncClient
, which is specifically designed for handling asynchronous requests. The asyncio.gather
function is used to execute a bunch of asynchronous tasks together, making it super fast. A friendly reminder: asynchronous requests must be used within a function defined with async def
, so don’t get it wrong!
JSON Data, No Problem!
Many websites now use JSON to transmit data, and Httpx makes handling JSON very convenient:
import httpx
import json
response = httpx.get('https://api.example.com/data')
data = response.json() # Directly converts to a Python dictionary, nice!
print(data['key']) # Access whatever you need
response.json()
directly converts the JSON string into a Python dictionary, making it very comfortable to use.
Timeout Settings, Reliable!
With networks, you never know when things might hang. Setting a timeout is very important!
import httpx
try:
response = httpx.get('https://www.example.com', timeout=5.0) # 5 seconds timeout
response.raise_for_status() # Check for errors
except httpx.TimeoutException:
print("Timeout occurred!")
except httpx.HTTPStatusError as exc:
print(f"Request error: {exc}")
Here we set a 5-second timeout; if there is no response after 5 seconds, an exception is raised. The raise_for_status()
function checks the HTTP status code to see if there are errors like 404 or 500.
Custom Headers, Change as You Wish!
Sometimes, you need to add some extra information to your requests, like User-Agent or Cookies. Httpx supports this as well:
import httpx
headers = {'User-Agent': 'My Custom Agent'}
response = httpx.get('https://www.example.com', headers=headers)
Just pass the headers in, and you’re done, simple and clear.
Httpx is a fantastic tool! It works for both synchronous and asynchronous requests, handles JSON easily, has reliable timeout settings, and allows for flexible header modifications. Once you master it, making network requests will be a breeze, and you won’t have to worry about various odd issues anymore!
Like and Share
Let money and love flow to you