# aiohttp

**aiohttp** 是面向 asyncio 与 Python 的异步 HTTP 客户端/服务器框架，在同一个包中同时提供用于发起请求的客户端、完整的 Web 服务器，以及客户端与服务器两种角色下的 [WebSocket](https://www.edgepedia.org/zh/websocket) 支持。<sup>[1](https://github.com/aio-libs/aiohttp)</sup><sup> • </sup><sup>[2](https://docs.aiohttp.org/en/stable/)</sup> 它于 2013 年首次发布，是 Python 最早的异步 HTTP 库之一，并且完全专注于异步操作。<sup>[3](https://github.com/OuYangMinOa/CompareHttpx/blob/main/benchmark_report.md)</sup> 该项目在 GitHub 上由 aio-libs 组织开发，采用 Apache-2.0 许可证，约有 16.6k 颗星。<sup>[1](https://github.com/aio-libs/aiohttp)</sup>

| 关键事实 | 详情 |
|---|---|
| 它是什么 | 面向 asyncio 与 Python 的异步 HTTP 客户端/服务器框架<sup>[1](https://github.com/aio-libs/aiohttp)</sup> |
| 首次发布 | 2013 年；Python 最早的异步 HTTP 库之一<sup>[3](https://github.com/OuYangMinOa/CompareHttpx/blob/main/benchmark_report.md)</sup> |
| 当前版本 | 3.14.5，Apache-2.0 许可<sup>[2](https://docs.aiohttp.org/en/stable/)</sup><sup> • </sup><sup>[4](https://pypi.org/project/aiohttp/)</sup> |
| 核心特性 | 客户端与 HTTP 服务器，开箱即用的客户端与服务器 WebSocket，中间件、信号与可插拔路由<sup>[2](https://docs.aiohttp.org/en/stable/)</sup> |
| 依赖 | multidict、yarl 与 frozenlist；aiodns 为可选项，推荐用于更快的 DNS 解析<sup>[1](https://github.com/aio-libs/aiohttp)</sup> |
| 连接池 | TCPConnector 默认连接数上限为 100，DNS 缓存 TTL 为 10 秒<sup>[5](https://docs.aiohttp.org/en/stable/client_reference.html?highlight=timeout)</sup> |
| 实测速度 | 在公开 API 基准测试中为四款受测客户端中最快，平均响应时间 0.190 秒<sup>[3](https://github.com/OuYangMinOa/CompareHttpx/blob/main/benchmark_report.md)</sup> |

## 客户端与服务器

客户端 API 以 `aiohttp.ClientSession` 为核心，由它管理连接池。文档中的快速入门范式是：打开一个会话，发出 GET 请求，并在嵌套的 `async with` 块中读取响应体。<sup>[2](https://docs.aiohttp.org/en/stable/)</sup> 由于会话会维护其连接池，项目文档建议不要在需要时临时创建会话，而应在一处创建持久会话——例如在应用的清理上下文中——以便复用连接、提升性能。<sup>[6](https://github.com/aio-libs/aiohttp/blob/master/docs/client_advanced.rst)</sup>

服务器端本身就是一个 Web 框架。请求处理器是一个协程，接受 `Request` 并返回 `Response`；处理器通过路由表或装饰器注册到 `Application` 上，应用则通过 `web.run_app()` 运行。<sup>[7](https://docs.aiohttp.org/en/stable/web_quickstart.html)</sup> Web 服务器包含中间件、信号与可插拔路由，客户端和服务器都支持 WebSocket，且无需回调风格的代码。<sup>[2](https://docs.aiohttp.org/en/stable/)</sup>

**连接管理。** `TCPConnector` 实现连接池，默认 `limit` 为 100 个并发连接，DNS 缓存 TTL 为 10 秒，并支持可配置的 keepalive 超时。<sup>[5](https://docs.aiohttp.org/en/stable/client_reference.html?highlight=timeout)</sup> aiohttp 不支持 HTTP 或 HTTPS 流水线。<sup>[6](https://github.com/aio-libs/aiohttp/blob/master/docs/client_advanced.rst)</sup> 由于 asyncio 的内部机制，关闭会话后底层连接会短暂保持打开；对于 SSL 会话，文档建议在停止事件循环前等待约 250 毫秒，以避免出现 `ResourceWarning: unclosed transport` 消息。<sup>[6](https://github.com/aio-libs/aiohttp/blob/master/docs/client_advanced.rst)</sup>

## 性能

基准测试一致将 aiohttp 列为最快的 Python HTTP 客户端之一，不过差距大小取决于工作负载。一项针对七个公开 API 端点、比较四个库的测试测得平均响应时间为：aiohttp 0.190 秒，异步模式下的 httpx 0.233 秒，requests 0.479 秒，同步模式下的 httpx 0.588 秒，aiohttp 最快，比最好的同步库快约 151.5%。<sup>[3](https://github.com/OuYangMinOa/CompareHttpx/blob/main/benchmark_report.md)</sup>

一项在 Python 3.13.7 上针对多进程服务器的本地 HTTP/1.1 基准测试测得：不使用连接池时，aiohttp 串行请求平均每次 0.265 毫秒；使用连接池时为 0.090 毫秒，提升 66.1%；在每批 100 个请求的并行测试中，连接池将每批平均耗时从 10.069 毫秒降至 4.721 毫秒。同一基准测试发现，启用连接池后 httpx 在并行模式下反而更慢，而 aiohttp 在两种模式下都从中受益。<sup>[8](https://github.com/ftnext/gfx-benchmark-python-httpx-vs-aiohttp)</sup> aiohttp 项目本身并不公布自己的数字，而是引导读者去看 asyncio 社区的基准测试 wiki。<sup>[1](https://github.com/aio-libs/aiohttp)</sup>

## 与 httpx 的比较

这两个库是主要的 asyncio 原生 HTTP 客户端，它们在 API 哲学与特性上各有不同。httpx 提供 HTTP/2 支持、与异步客户端一并提供的同步客户端、通过 anyio 实现的 trio 支持、用于进程内测试的 ASGI/WSGI 传输，以及传输层重试。aiohttp 提供 WebSocket 客户端、完整的 HTTP 服务器，以及在高吞吐服务中长期的生产实践历史。<sup>[9](https://www.async-concurrency.com/network-io-protocol-handling/async-http-clients-servers/choosing-between-httpx-and-aiohttp/)</sup> aiohttp 的核心不支持 HTTP/2；要启用它需要第三方插件。<sup>[9](https://www.async-concurrency.com/network-io-protocol-handling/async-http-clients-servers/choosing-between-httpx-and-aiohttp/)</sup> 一项在 Python 3.15 上针对本地 NGINX 服务器的吞吐量基准测试报告称，在 1 KB 载荷下，aiohttp 3.11.3 为 12,850 请求/秒，而异步 httpx 为 14,200 请求/秒；在该环境中，aiohttp 的 TLS 终止开销为 18%，而 httpx 为 9%。<sup>[10](https://www.johal.in/we-tested-python-315-http-clients-requests-vs)</sup>

## 安全

该项目维护安全策略并在 GitHub 上发布公告。过往公告包括 GHSA-45c4-8wx5-qw6w（CVE-2023-37276），即通过头部值中的 CR/LF/NUL 进行 HTTP 请求走私，已在 3.8.5 中修复。<sup>[11](https://github.com/aio-libs/aiohttp/blob/master/THREAT_MODEL.md)</sup> 较近的公告有：GHSA-mfx4-hv73-q22v 描述了通过 WebSocket 升级流程中的边界情况实施的 HTTP 请求走私，已在 3.14.2 中修复；GHSA-cq5v-8q36-5273 描述了 C 语言编写的 HTTP 响应解析器中的越界堆读取，可能导致拒绝服务。<sup>[12](https://github.com/aio-libs/aiohttp/security/advisories)</sup> 项目的威胁模型文档列举了诸如分帧完整性之类的目标，即一条线上消息对应一条解析后的消息，解析器所接受的任何内容都不会导致对端之间失步。<sup>[11](https://github.com/aio-libs/aiohttp/blob/master/THREAT_MODEL.md)</sup>

## 安装与生态

aiohttp 可通过 `pip install aiohttp` 安装，可选的 `aiohttp[speedups]` 附加包会捆绑 aiodns 与 Brotli，以加快 DNS 解析并提供压缩支持。<sup>[2](https://docs.aiohttp.org/en/stable/)</sup> 它支持 Python 3.10 至 3.14。<sup>[4](https://pypi.org/project/aiohttp/)</sup> 生态中包括 aiohttp-demos、aiohttp-things 等配套项目，AsyncIO 社区在官方 wiki 上维护了一份基准测试列表，供对效率比较感兴趣的读者参考。<sup>[1](https://github.com/aio-libs/aiohttp)</sup>

## References

1. [aio-libs/aiohttp on GitHub](https://github.com/aio-libs/aiohttp)
2. [Welcome to AIOHTTP, aiohttp 3.14.5 documentation](https://docs.aiohttp.org/en/stable/)
3. [HTTP Libraries Performance Comparison, CompareHttpx benchmark report](https://github.com/OuYangMinOa/CompareHttpx/blob/main/benchmark_report.md)
4. [aiohttp on PyPI](https://pypi.org/project/aiohttp/)
5. [Client Reference, aiohttp documentation](https://docs.aiohttp.org/en/stable/client_reference.html?highlight=timeout)
6. [client_advanced.rst, aiohttp documentation source](https://github.com/aio-libs/aiohttp/blob/master/docs/client_advanced.rst)
7. [Web Server Quickstart, aiohttp documentation](https://docs.aiohttp.org/en/stable/web_quickstart.html)
8. [gfx-benchmark-python-httpx-vs-aiohttp](https://github.com/ftnext/gfx-benchmark-python-httpx-vs-aiohttp)
9. [Choosing Between httpx and aiohttp, async-concurrency.com](https://www.async-concurrency.com/network-io-protocol-handling/async-http-clients-servers/choosing-between-httpx-and-aiohttp/)
10. [We Tested 6 Python 3.15 HTTP Clients, johal.in](https://www.johal.in/we-tested-python-315-http-clients-requests-vs)
11. [THREAT_MODEL.md, aio-libs/aiohttp](https://github.com/aio-libs/aiohttp/blob/master/THREAT_MODEL.md)
12. [Security Advisories, aio-libs/aiohttp](https://github.com/aio-libs/aiohttp/security/advisories)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Frameworks, libraries, and APIs › Web frameworks and libraries

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/aiohttp

English original: https://www.edgepedia.org/aiohttp
