“Requests allows us to elegantly send requests synchronously, while aiohttp opens the door to the asynchronous world.”
๐ง What is aiohttp?
<span>aiohttp</span> is the most popular asynchronous HTTP client and server framework in Python, built on top of <span>asyncio</span>. It performs excellently in scenarios involving high concurrency requests, real-time communication, and microservice interfaces.
You can think of it as:
- An asynchronous version of
<span>requests</span>(client) - A lightweight asynchronous version of
<span>Flask</span>(server)
๐ฆ Quick Installation
pip install aiohttp
๐ Practical Application Scenarios
We will master <span>aiohttp</span> through the following four real scenarios:
- Concurrent web scraping (client)
- Asynchronous API aggregation (client)
- Building a RESTful API (server)
- Setting up a WebSocket communication service (server)
โ Scenario 1: Asynchronous Concurrent Web Scraping
Compared to <span>requests</span>, which can only send one request at a time, <span>aiohttp</span> can send dozens or even hundreds of requests simultaneously.
import asyncio
import aiohttp
async def fetch(url, session):
async with session.get(url) as response:
return await response.text()
async def main():
urls = [f"https://httpbin.org/delay/1?i={i}" for i in range(10)]
async with aiohttp.ClientSession() as session:
tasks = [fetch(url, session) for url in urls]
responses = await asyncio.gather(*tasks)
print(f"Fetched {len(responses)} web pages")
asyncio.run(main())
๐งช Test Result: 10 requests completed in about 1 second (nearly 10 times faster than synchronous).
โ Scenario 2: API Aggregation Middleware
async def get_weather(session):
async with session.get("https://api.weatherapi.com/xxx") as resp:
return await resp.json()
async def get_news(session):
async with session.get("https://api.newsapi.org/xxx") as resp:
return await resp.json()
async def aggregate_data():
async with aiohttp.ClientSession() as session:
weather, news = await asyncio.gather(
get_weather(session),
get_news(session)
)
return {"weather": weather, "news": news}
โ Suitable for aggregating multiple third-party APIs to return a unified result, significantly improving response speed.
โ Scenario 3: Building an Asynchronous RESTful Server
from aiohttp import web
async def handle(request):
return web.json_response({"message": "Hello aiohttp!"})
app = web.Application()
app.add_routes([web.get("/", handle)])
if __name__ == '__main__':
web.run_app(app, port=8080)
๐ Access: http://localhost:8080/
โ Scenario 4: WebSocket Real-Time Communication
from aiohttp import web
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == web.WSMsgType.TEXT:
await ws.send_str(f"You said: {msg.data}")
return ws
app = web.Application()
app.router.add_get("/ws", websocket_handler)
web.run_app(app)
๐ฌ After the client connects, real-time bidirectional communication is possible, making it ideal for:
- Chat systems
- Real-time data push
- Collaborative editing
๐ก๏ธ Some Advanced Features of aiohttp
- Supports connection pools, timeouts, and retry mechanisms
- Supports middleware (interceptors)
- Supports Cookies, Headers, and authentication mechanisms
- Perfectly integrates with
<span>asyncio</span>for task queues and timeout control
๐ง aiohttp vs requests vs httpx
| Feature | requests โ | aiohttp โ | httpx โ |
|---|---|---|---|
| Sync Support | โ | โ | โ |
| Async Support | โ | โ | โ |
| Client Performance | Medium | High | High |
| Server Functionality | โ | โ | โ |
| WebSocket Support | โ | โ | โ |
๐ If you are using asyncio, choose aiohttp; in the synchronous world, requests still reign supreme.
โ Practical Tips Collection
-
Set Timeout
timeout = aiohttp.ClientTimeout(total=5) async with aiohttp.ClientSession(timeout=timeout) as session: ... -
Set Connection Pool Size
connector = aiohttp.TCPConnector(limit=10) session = aiohttp.ClientSession(connector=connector) -
Set Retry Mechanism (with tenacity)
from tenacity import retry @retry async def robust_fetch(...): ... -
Packet Capture Debugging
import logging logging.basicConfig(level=logging.DEBUG)
๐ Recommended Learning Resources
- Official aiohttp Documentation
- “Python Asynchronous Programming in Practice”
- Project Recommendation: FastAPI (built on Starlette + aiohttp)
๐ Conclusion
<span>aiohttp</span>is one of the most mature asynchronous HTTP frameworks in Python.- Supports high-concurrency client scraping, API aggregation, WebSocket, and API microservices.
- Lightweight and efficient, suitable for various scenarios such as asynchronous microservice architecture, scraping platforms, and real-time systems.
If you are using asyncio but haven’t used aiohttp, it’s like having a car but still walking.
If you found this helpful,๐ please click “Like” + Save and feel free to follow the WeChat public account “Python Module Master” for more practical content ๐