python3从入门到精通(七): 异步HTTP框架之aiohttp模块

简谈异步爬虫aiohttp 之前涉及到的爬虫,都基本上使用的requests库进行爬取。但是request获取网站内容的话,是有相应时间的,要不然也不会设置timeout了。但是,响应时间内,程序也在等待,响应时间越长等待时间就相对变长。因为python的自身原因多线程就不做最优选了(但是我很喜欢用=)),多进程则是利用cpu的多核在同一时间内进行多个任务。因此就该引出一个概念——协程coroutine。 阅读详情

一、aiohttp 模块

aiohttp 是Python中 基于asyncio实现 的 异步HTTP框架,主要有两大核心能力:

异步 HTTP 客户端: 替代同步的requests,用于异步发送HTTP请求(GET/POST),适合高并发的网络请求场景,如爬虫、API调用
异步 HTTP 服务器: 快速搭建异步Web服务,替代部分同步的Flask/Django的场景,适合IO密集型的Web应用

核心优势:
异步非阻塞,能在单线程中处理大量并发请求,大幅提升 IO 密集型任务的效率。比如同时请求 100 个接口,无需等待前一个完成。适合高并发网络请求 / 轻量级 Web 服务

二、安装aiohttp

# 核心库
pip install aiohttp
# 安装加速依赖(可选,提升性能)
pip install aiohttp[speedups] 
# 可选:加速 DNS 解析
pip install aiodns
# 可选:加速 HTTP 解析
pip install cchardet
# 可选:性能监控
pip install aiohttp-devtools

三、异步 HTTP 客户端

作用是 发起异步 HTTP/HTTPS 请求(GET/POST/PUT 等),管理连接池、Cookie、超时等

* aiohttp.ClientSession(): 必须用 async with 管理,自动关闭连接,避免资源泄露 
* aiohttp.ClientTimeout(total=10): 设置超时,避免请求无限等待,total=10 表示总超时 10 秒

# 注意事项:
1. 避免在异步函数中调用同步阻塞函数,如 time.sleep()、requests.get(),会阻塞整个事件循环,改用 asyncio.sleep()
2. 复用 ClientSession:
ClientSession 是"可复用"的,不要为每个请求创建新的 ClientSession,一个业务流程中只创建一个ClientSession,利用连接池复用 TCP 连接,避免频繁创建 / 关闭导致的性能损耗
3. 异常处理:
即使开启raise_for_status,仍需捕获ClientError通用异常、网络异常、TimeoutError超时异常、JSON解析等
4. 必配 timeout: 
无论场景如何,必须配置超时,防止请求挂起阻塞事件循环

3.1、ClientSession 会话管理

ClientSession 是 aiohttp的核心 ,所有请求都通过它发起,且必须配合**async/await**使用。

ClientSession 是客户端的核心入口,用于管理 HTTP 会话的生命周期:
(1)内置连接池,复用 TCP 连接,减少握手开销
(2)自动维护 Cookie、请求头headers
(3)统一管理超时、代理、SSL 等配置
(4)必须通过 async with 异步上下文管理器使用,自动释放资源

3.1.1、初始化方法:ClientSession.__init__() 的核心参数

# 核心参数:
* base_url: "设置全局基础 URL"
  - 作用: 所有请求的路径会自动拼接在该地址后,避免重复书写相同的域名/接口前缀,简化代码
  - 默认值: None

* headers: "设置全局请求头,所有请求默认携带"
  - 若单次请求单独配置headers,会覆盖全局同名请求头(非同名则合并)
  - 典型场景: 统一设置 User-Agent、Accept、Content-Type、Token 等通用头
  - 默认值: None

*  cookies: "设置全局 Cookie,所有请求默认携带"
  - ClientSession会"自动维护"响应返回的 Cookie,无需手动更新
  - 典型场景: 请求需要登录态的接口,提前传入登录后的 Cookie
  - 默认值: None

* timeout: "设置全局超时规则,控制请求的生命周期"
  - 支持"简化配置"(直接传数字,总超时)和"精细化配置"(ClientTimeout,分阶段超时),单次请求可单独配置超时覆盖全局
  - "ClientTimeout""精细化配置参数":
    - connect=5: 建立TCP连接的超时时间
    - sock_read=10: 从套接字读取响应数据的超时时间,None=使用total值
    - sock_connect: 套接字连接超时,None=使用connect值
    - total=30: 整个请求的生命周期超时,None=无限制
  - "实际开发必须配置",避免因服务端无响应导致事件循环阻塞
  - 默认值: sentinel 默认不超时,易导致请求无限挂起

* connector: "配置底层连接池,控制最大并发连接数、每个域名最大连接数、DNS 缓存、SSL 验证等"
  - 高并发请求的核心优化参数
  - 默认值: None(自动创建默认TCPConnector,全局最大连接100)
  - "TCPConnector核心参数":
    - limit: 全局最大并发连接数,高并发建议根据服务器配置调整
      - 默认: 100,0=无限制
    - limit_per_host: 每个域名的最大连接数
      - 默认: 0=使用limit值,建议设置10-20
    - ttl_dns_cache: DNS缓存超时(秒)
      - 默认: 300,0=禁用缓存,适合动态域名
    - ssl: 是否验证SSL证书
      - 默认: True,测试环境可设False 忽略自签名证书
    - proxy: 全局代理地址
# 注意:
TCPConnector不能写在全局作用域,必须在运行中的事件循环里创建,如果写在全局,程序启动时还没有事件循环,直接报错
# Windows系统需要显式设置事件循环策略,解决RuntimeError: no running event loop
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

* proxy: "设置全局代理,所有请求默认通过该代理发起"
  - 支持HTTP 代理和SOCKS5 代理,单次请求可单独配置 proxy 覆盖全局
  - 默认值: None

* proxy_auth: "为全局代理配置基础认证(用户名 + 密码),适用于需要账号密码的代理服务器"
  - 默认值: None

* auth: 设置全局 HTTP 基础认证,所有请求自动在请求头添加Authorization: Basic xxx,无需手动拼接认证头
  - 默认值: None

* raise_for_status: "设置自动触发异常"
  - 若响应状态码为4xx(客户端错误)/5xx(服务器错误),会自动调用resp.raise_for_status(),抛出aiohttp.ClientResponseError,无需手动判断状态码
  - 默认值: False

* auto_decompress: "是否自动解压响应内容,支持 gzip/deflate/brotli 三种压缩格式"
  - 默认值: True 开启,无需手动处理解压,直接读取原始内容即可
  - 若需要获取原始压缩数据,可设为False
3.1.1.1、核心参数示例
# base_url
async def main():
    # 配置基础URL为接口根地址
    async with aiohttp.ClientSession(base_url="https://api.httpbin.org") as session:
        # 实际请求:https://api.httpbin.org/get
        async with session.get("/get") as resp:
            print(await resp.json())
        # 实际请求:https://api.httpbin.org/post
        async with session.post("/post", json={"key": "val"}) as resp:
            print(await resp.json())

# headers
async def main():
    # 全局请求头: 统一UA+指定接收JSON+携带令牌
    global_headers = {
        "User-Agent": "Python-Aiohttp/3.9.1",
        "Accept": "application/json",
        "X-Token": "abc123def456"
    }
    async with aiohttp.ClientSession(headers=global_headers) as session:
        # 单次请求覆盖全局的Accept,新增Referer(最终头为合并+覆盖)
        async with session.get("/get", headers={"Accept": "*/*", "Referer": "https://xxx.com"}) as resp:
            print(dict(resp.request_info.headers))  # 查看最终发送的请求头

# Cookie
async def main():
    # 全局Cookie:携带登录态uid和token
    global_cookies = {"uid": "10086", "login_token": "xyz789"}
    async with aiohttp.ClientSession(cookies=global_cookies) as session:
        async with session.get("/cookies") as resp:
            print(await resp.json())  # 查看服务端接收到的Cookie

# Timeout
# 简化配置:直接传数字,指定总超时,整个请求从连接到读取完成的总时间
async def main():
    # 全局总超时10秒:所有请求若10秒内未完成则抛超时异常
    async with aiohttp.ClientSession(timeout=10) as session:
        async with session.get("/delay/15") as resp:  # 接口延迟15秒返回
            print(await resp.json())  # 会抛出asyncio.TimeoutError
# 精细化配置:用ClientTimeout分阶段控制(推荐,更灵活)
from aiohttp import ClientTimeout
async def main():
    # 精细化超时配置:连接5秒+读取10秒+总超时30秒
    timeout = ClientTimeout(
        connect=5,       # 建立TCP连接的超时时间
        sock_read=10,    # 从套接字读取响应数据的超时时间
        total=30         # 整个请求的生命周期超时(兜底)
    )
    async with aiohttp.ClientSession(timeout=timeout) as session:
        async with session.get("/delay/8") as resp:
            print(await resp.json())  # 读取超时(sock_read=10秒可正常,delay=12则超时)

# connector  
# 高并发 + 禁用 DNS 缓存
from aiohttp import TCPConnector
async def main():
    # 配置连接池:全局最大200连接,每个域名20连接,禁用DNS缓存
    connector = TCPConnector(
        limit=200,
        limit_per_host=20,
        ttl_dns_cache=0
    )
    async with aiohttp.ClientSession(connector=connector) as session:
        # 高并发请求时,连接池会自动复用TCP连接,减少握手开销
        tasks = [session.get("/get") for _ in range(30)]
        results = await asyncio.gather(*tasks)
        print(f"成功请求{len(results)}次")
# 忽略SSL 证书验证,测试环境专用
async def main():
    # 禁用SSL验证(生产环境禁止使用!)
    connector = TCPConnector(ssl=False)
    async with aiohttp.ClientSession(connector=connector) as session:
        async with session.get("https://自签名证书的域名.com") as resp:
            print(resp.status)

# proxy
async def main():
    async with aiohttp.ClientSession(
        # HTTP代理(根据实际代理地址修改)
        proxy="http://127.0.0.1:7890"
        # SOCKS5代理:
        # proxy="socks5://127.0.0.1:7890"
    ) as session:
        async with session.get("/ip") as resp:
            print(await resp.json())  # 查看代理后的出口IP

# proxy_auth
from aiohttp import BasicAuth
async def main():
    async with aiohttp.ClientSession(
        proxy="http://127.0.0.1:7890",
        # 代理认证:用户名admin,密码123456
        proxy_auth=BasicAuth(login="admin", password="123456")
    ) as session:
        async with session.get("/ip") as resp:
            print(await resp.json())

# auth
from aiohttp import BasicAuth
async def main():
    async with aiohttp.ClientSession(
        # HTTP基础认证:用户名user,密码pass123
        auth=BasicAuth("user", "pass123")
    ) as session:
        async with session.get("/basic-auth/user/pass123") as resp:
            print(await resp.json())  # 鉴权成功返回{"authenticated":true,"user":"user"}

# raise_for_status
async def main():
    async with aiohttp.ClientSession(raise_for_status=True) as session:
        try:
            # 响应状态码404,会自动抛异常
            async with session.get("/status/404") as resp:
                data = await resp.json()  # 不会执行到这一步
        except aiohttp.ClientResponseError as e:
            print(f"请求失败:{e.status} - {e.message}")  # 捕获并处理异常

# auto_decompress
async def main():
    # 关闭自动解压,获取原始gzip压缩数据
    async with aiohttp.ClientSession(auto_decompress=False) as session:
        async with session.get("/gzip") as resp:
            raw_data = await resp.read()  # 原始二进制压缩数据
            print(f"原始数据长度:{len(raw_data)}")
    
    # 开启自动解压(默认),直接读取解压后的文本
    async with aiohttp.ClientSession(auto_decompress=True) as session:
        async with session.get("/gzip") as resp:
            text_data = await resp.text()  # 已解压的文本
            print(f"解压后文本长度:{len(text_data)}")
3.1.1.1、核心参数组合示例

结合高并发 + 超时控制 + 全局配置 + 代理 + 异常自动触发,可作为生产环境可用的ClientSession配置模板

import asyncio
import aiohttp
from aiohttp import ClientTimeout, TCPConnector, BasicAuth

async def fetch_url(session, url):
    try:
        async with session.get(url) as response:
            return await response.json()
    except Exception as e:
        return f"请求失败: {str(e)}"

async def main():
    # 1. 精细化超时配置
    timeout = ClientTimeout(
        connect=5,     # 建立TCP连接的超时时间
        sock_read=10,  # 从套接字读取响应数据的超时时间
        total=20       # 整个请求的生命周期超时
    )
    # 2. 高并发连接池配置
    connector = TCPConnector(
        limit=200,          # 全局最大并发连接数
        limit_per_host=20,  # 每个域名的最大连接数
        ttl_dns_cache=0     # DNS 缓存超时
    )
    # 3. 全局请求头
    headers = {
        "User-Agent": "Python-Aiohttp/3.9.1",
        "Accept": "application/json",
        "X-App-Version": "1.0.0"
    }
    # 4. 初始化ClientSession
    try:
        async with aiohttp.ClientSession(
            base_url="https://api.example.com",
            headers=headers,
            timeout=timeout,
            connector=connector,
            proxy="http://proxy:8080", # 按需开启代理
            # proxy_auth=BasicAuth("admin", "123456"),  # 代理需要认证时开启
            # auth=BasicAuth("user", "pass123"),  # 接口需要基础认证时开启
            raise_for_status=True,
            auto_decompress=True,
            trust_env=False

        ) as session:
            # 高并发请求10个接口
            tasks = [fetch_url(session, f"/get?num={i}") for i in range(10)]
            results = await asyncio.gather(*tasks, return_exceptions=True)
            return results
    except Exception as eg:
        print(f"Partial failures: {eg}")

if __name__ == '__main__':
    asyncio.run(main())

3.1.2、ClientSession 核心请求方法

GET / POST / PUT / DELETE / HEAD / OPTIONS
所有请求方法的参数结构一致,核心参数通用,仅 HTTP 方法不同
以下以 session.get() 为例详解

Content_Type 一般不用单独设置,aiohttp 会自动生成

# 核心参数:
* url: 请求目标 URL
  - 类型: str/URL

* params: URL查询参数 "GET请求"
  - 类型: dict/list/tuple/str
  - 自动拼接为: "?key1=val1&key2=val2"

* data: 请求体的表单/二进制数据 "POST/PUT等请求"
  - 类型: bytes/dict/FormData

* json: JSON格式请求体
  - 自动序列化,设置 Content-Type: application/json
  - 类型: dict/list

* headers: 单次请求头 "覆盖全局headers"
  - 类型: dict

* cookies: 单次请求 Cookie "新增/覆盖全局CookieJar"
  - 类型: dict

* timeout: 单次请求超时 "覆盖全局timeout"
  - 类型: ClientTimeout/int

* proxy: 代理地址
  - http://127.0.0.1:7890 或者 socks5://127.0.0.1:7890

* files: 上传文件 "POST/PUT常用"
  - 类型: dict

* ssl: 是否开启SSL验证
  - False: 禁用验证,适合自签名证书
  - True: 开启验证,默认
  - 类型: bool/SSLContext

* allow_redirects: 是否允许重定向(3xx 响应)
3.1.2.1、GET 请求
import asyncio
import aiohttp
# 全局超时配置
timeout = aiohttp.ClientTimeout(total=10)

async def demo_get(session):
    params = {"name": "张三", "age": 20}  # 自动拼接为?name=张三&age=20

    # 单次请求头(覆盖全局)
    headers = {"Accept": "application/json"}

    try:
        # 2. 发起 GET 请求,await 等待响应
        async with session.get(
            url="https://httpbin.org/get",
            params=params,
            headers=headers,
            timeout=timeout
        ) as response:
            if response.status == 200:
                text = await response.text()  # 获取文本响应,如 HTML/JSON 字符串
                # json_data = await response.json()  # 如果响应是 JSON,用这个
                # binary_data = await response.read()  # 获取二进制数据(如图片)
                return text
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")

async def main():
    # 1. 创建 ClientSession(异步上下文管理器),自动管理连接池
    async with aiohttp.ClientSession(
            headers={"User-Agent": "aiohttp-demo/1.0"},  # 全局请求头
            timeout=timeout
    ) as session:
        await demo_get(session)

if __name__ == '__main__':
    result = asyncio.run(main())
3.1.2.2、POST 请求
import asyncio
import aiohttp
# 全局超时配置
timeout = aiohttp.ClientTimeout(total=10)

async def demo_post(session):
    # 2.1 JSON 请求体
    json_data = {"name": "张三", "age": 20}
    try:
        async with session.post(
            url="https://httpbin.org/post",
            json=json_data,  # 自动序列化 JSON,设置 Content-Type: application/json
            timeout=timeout
        ) as response:
            if response.status == 200:
                return await response.json()
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")

    # 2.2 表单请求体(设置 Content-Type: application/x-www-form-urlencoded)
    form_data = {"gender": "male", "city": "Beijing"}
    try:
        async with session.post(
            url="https://httpbin.org/post",
            data=form_data,
            timeout=timeout
        ) as response:
            if response.status == 200:
                return await response.json()
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")

    # 2.3 多表单/文件上传(设置 Content-Type: multipart/form-data; boundary=----)
    multipart_data = aiohttp.FormData()
    multipart_data.add_field("username", "user")
    multipart_data.add_field("password", "pass123456")
    multipart_data.add_field("text_field", "测试文本")
    # 上传文件(本地文件路径)
    multipart_data.add_field(
        "file_field",
        open("demo.txt", "rb"),  # 二进制模式打开
        filename="demo.txt",  # 文件名
        content_type="text/plain"  # MIME类型
    )
    try:
        async with session.post(
            url="https://httpbin.org/post",
            data=multipart_data,
            timeout=timeout
        ) as response:
            if response.status == 200:
                return await response.json()
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")

async def main():
    # 1. 创建 ClientSession(异步上下文管理器),自动管理连接池
    async with aiohttp.ClientSession(
            headers={"User-Agent": "aiohttp-demo/1.0"},  # 全局请求头
            timeout=timeout
    ) as session:
        await demo_post(session)
if __name__ == '__main__':
    result = asyncio.run(main())
3.1.2.3、并发请求

多个请求几乎同时发起,总耗时≈最慢的那个请求的耗时,而非所有请求耗时之和

import asyncio
import aiohttp
async def fetch_url(session, url):
    async with session.get(url) as response:
        if response.status != 200:
            raise Exception(f"Error fetch {url}; status code: {response.status}")
        # return await response.text()
        return response.status

async def main():
    url_list = [
        "https://httpbin.org/get?num=1",
        "https://httpbin.org/get?num=2",
        "https://httpbin.org/get?num=3"
    ]
    try:
        async with aiohttp.ClientSession() as session:
            tasks  =[fetch_url(session, url) for url in url_list]
            results = await asyncio.gather(*tasks, return_exceptions=True)
    except Exception as eg:
        print(f"Partial failures: {eg}")

    for url, result in zip(url_list, results):
        print(f"{url}: {result}")
if __name__ == '__main__':
    asyncio.run(main())
3.1.2.4、请求重试(配合 tenacity)
import aiohttp
import asyncio
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
# 重试规则:最多3次,指数退避(1s→2s→4s),仅捕获ClientError
@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=1, max=4),
    retry=retry_if_exception_type(aiohttp.ClientError)
)
async def fetch_with_retry(session, url):
    async with session.get(url, timeout=5) as resp:
        resp.raise_for_status()
        return await resp.text()

async def main():
    async with aiohttp.ClientSession() as session:
        try:
            result = await fetch_with_retry(session, "https://httpbin.org/get")
            print("请求成功:", result[:50])
        except aiohttp.ClientError as e:
            print("3次重试后仍失败:", e)
asyncio.run(main())

3.1.3、ClientSession 核心属性

# 核心属性:
* connector: 当前会话连接池管理器,可"查看/动态调整"连接池配置,如临时修改最大连接数

* timeout: 会话的全局超时配置
  - 初始化时传入的 timeout 参数,若传数字会自动转为 ClientTimeout 对象

* cookie_jar: 会话的 Cookie 管理器,负责存储、发送、更新 Cookie,是"保持登录态的核心"
  - session.cookie_jar.update_cookies({"token": "123"}): 手动添加/更新 Cookie(cookies为字典)
  - clear(): 清空所有 Cookie
  - filter_cookies(url): 筛选指定 URL 可发送的 Cookie

* closed: 会话是否已关闭
  - True: 表示关闭,无法再发起请求
  - False: 表示正常运行

* headers: 只读,会话的全局请求头,底层是有序字典,支持"大小写不敏感的键查找"
# base_url
async def main():
    async with aiohttp.ClientSession(base_url="https://api.httpbin.org") as session:
        # 查看基础URL(yarl.URL对象,可转字符串)
        print("基础URL(原始):", session._base_url)
        print("基础URL(字符串):", str(session._base_url))
        # 拼接路径(等价于 session.get("/get"))
        full_url = session._base_url / "get"
        print("拼接后的完整URL:", full_url)

# headers
async def main():
    global_headers = {"User-Agent": "Aiohttp-Demo", "X-Token": "123456"}
    async with aiohttp.ClientSession(headers=global_headers) as session:
        # 查看全局请求头(整体)
        print("全局请求头:", dict(session.headers))
        # 动态修改请求头
        session.headers["User-Agent"] = "Demo-Aiohttp"
        # 按键查找(大小写不敏感)
        print("User-Agent:", session.headers.get("user-agent"))
        print("X-Token:", session.headers["X-Token"])

# timeout
from aiohttp import ClientTimeout
async def main():
    timeout = ClientTimeout(connect=5, sock_read=10, total=30)
    async with aiohttp.ClientSession(timeout=timeout) as session:
        # 查看全局超时配置
        print("全局超时对象:", session.timeout)
        print("连接超时(秒):", session.timeout.connect)
        print("读取超时(秒):", session.timeout.sock_read)
        print("总超时(秒):", session.timeout.total)

# connector
from aiohttp import TCPConnector
async def main():
    connector = TCPConnector(limit=200, limit_per_host=20)
    async with aiohttp.ClientSession(connector=connector) as session:
        # 查看连接池配置
        print("连接池类型:", type(session.connector))
        print("全局最大连接数:", session.connector.limit)
        print("单域名最大连接数:", session.connector.limit_per_host)
        # 动态修改连接池配置(慎用,需确保线程安全)
        session.connector.limit = 150
        print("修改后全局最大连接数:", session.connector.limit)

# closed
async def main():
    session = aiohttp.ClientSession()
    # 初始状态:未关闭
    print("会话是否关闭(初始):", session.closed)
    # 发起请求
    async with session.get("https://api.httpbin.org/get") as resp:
        print("请求状态码:", resp.status)
    # 手动关闭会话
    await session.close()
    print("会话是否关闭(关闭后):", session.closed)
    
    # 尝试对已关闭的会话发起请求(会抛异常)
    try:
        async with session.get("https://api.httpbin.org/get") as resp:
            pass
    except RuntimeError as e:
        print("异常:", e)

# cookie
async def main():
    async with aiohttp.ClientSession() as session:
        # 1. 手动添加Cookie
        session.cookie_jar.update_cookies(
            {"uid": "10086", "login_token": "abc789"},
            url="https://api.httpbin.org"  # 指定Cookie生效的URL(可选)
        )   
        # 2. 查看所有Cookie
        print("会话所有Cookie:", session.cookie_jar._cookies)
        # 3. 筛选指定URL的Cookie
        filtered_cookies = session.cookie_jar.filter_cookies("https://api.httpbin.org")
        print("指定URL的Cookie:", {k: v.value for k, v in filtered_cookies.items()})
        # 4. 发起请求,验证Cookie是否携带
        async with session.get("https://api.httpbin.org/cookies") as resp:
            print("服务端接收的Cookie:", await resp.json())
        # 5. 清空Cookie
        session.cookie_jar.clear()
        print("清空后Cookie:", session.cookie_jar._cookies)

3.2、ClientResponse 响应对象

每次发起请求后,会返回 ClientResponse 对象,封装所有响应数据

# 核心方法: 
"均需 await"
* text(encoding=None): 读取响应文本,自动解码,encoding指定编码,如utf-8
* json(encoding=None, loads=json.loads): 解析JSON响应,失败抛出JSONDecodeError异常
* read(): 读取二进制响应(适合图片/视频/文件)
* release(): 手动释放响应资源
  - async with会自动调用,无需手动
* raise_for_status(): 手动触发 4xx/5xx 响应的异常(HTTPError)
* content.read(n=-1): 流式读取响应
  - n: 指定读取字节数,-1 读取全部,适合大文件
# 核心属性:
"无需 await"
* status: HTTP 响应状态码(200=成功,404=资源未找到,500=服务器错误)
* reason: 状态码描述,200→"OK",404→"Not Found"
* headers: 响应头,字典格式 如: resp.headers["Content-Type"]
* cookies: 响应返回的Cookie 如: resp.cookies["token"].value
* content_type: 响应内容类型 如: application/json、text/html
* url: 最终请求的 URL(含重定向后的地址)
* history: 重定向历史 如: 301/302跳转记录

3.2.1、流式读取大文件

import aiohttp
import asyncio
async def download_large_file():
    async with aiohttp.ClientSession() as session:
        async with session.get("https://httpbin.org/stream/100", timeout=60) as resp:
            # 流式读取(每次1024字节)
            with open("large_file.txt", "wb") as f:
                while True:
                    chunk = await resp.content.read(1024)
                    if not chunk:
                        break
                    f.write(chunk)
    print("大文件下载完成")
asyncio.run(download_large_file())

四、异步 HTTP 服务器

搭建轻量级异步 Web 服务,处理路由、请求、响应、WebSocket 等

4.1、web.Application 核心类

创建异步 Web 应用实例,管理路由、中间件、配置、信号(生命周期事件)

4.1.1、初始化方法:web.Application(**kwargs)

参数名类型默认值含义
middlewareslist[]中间件列表(全局请求 / 响应拦截,如日志、鉴权)
routerweb.UrlDispatcherUrlDispatcher()路由管理器(可自定义路由规则)
client_max_sizeint1024*1024最大请求体大小(默认 1MB,超出抛异常)
debugboolFalse调试模式(开启后显示详细错误栈)

4.1.2、web.Application的核心方法

方法名作用
add_routes(routes)添加路由规则(接收 RouteTableDef 或路由列表)
router.add_get(path, handler)快捷添加 GET 路由(post/put/delete 同理)
run_app(app, **kwargs)运行应用(需导入 from aiohttp import web)

4.2、web.Request 请求对象

web.Request 是 aiohttp 服务器端处理请求的核心对象,每个路由处理函数的第一个参数就是该对象,封装了客户端发起的所有请求数据(URL、参数、请求体、头信息、Cookie 等)

4.2.1、web.Request 核心属性 “无需await”

4.2.1.1、path 和 path_qs
* path: 请求的路径 "不含域名 和 查询参数"
* path_qs: 请求路径 + 查询参数,完整路径 "不含域名"
# 访问 http://127.0.0.1:8080/user/1001?name=张三
from aiohttp import web
async def handler(request):
    print("path请求路径:", request.path)  # /user/1001
    print("path_as请求路径:", request.path_as)  # /user/1001?name=张三
    return web.Response(text=f"Path: {request.path}")
app = web.Application()
app.add_routes([web.get("/user/{user_id}", handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.1.2、query

URL查询参数(即**?key1=val1&key2=val2**部分),支持大小写不敏感查找

# 常用方法:
* get(key, default=None): 获取单个值,避免键不存在报错
* getall(key): 获取同名多值
# 访问 http://127.0.0.1:8080/query?name=张三&tag=1&tag=2
async def handler(request):
    # 1. 获取单个查询参数
    name = request.query.get("name", "未知用户")
    age = request.query.get("age", 0)  # 字符串类型,需手动转int

    # 2. 获取同名多值(如 ?tag=1&tag=2&tag=3)
    tags = request.query.getall("tag")  # 返回列表:["1", "2", "3"]

    # 3. 转为普通字典(同名多值仅保留第一个)
    query_dict = dict(request.query)  # 核心修正:替代不存在的 to_dict()
    # 等价写法:query_dict = dict(request.query.items())

    # 4. 手动处理同名多值,转为 {key: list} 格式的字典
    query_multi_dict = {}
    for key in request.query.keys():
        query_multi_dict[key] = request.query.getall(key)
    return web.json_response({
        "single_value": {
            "name": name,
            "age": int(age)  # 手动转换类型
        },
        "multi_value": {
            "tags": tags
        },
        "query_dict": query_dict,  # 单值字典
        "query_multi_dict": query_multi_dict  # 多值字典
    })
app = web.Application()
app.add_routes([web.get("/query", handler)])
if __name__ == "__main__":
    web.run_app(app, host="127.0.0.1", port=8080)
4.2.1.3、match_info

路由路径参数(即 /{user_id} 中的动态部分),通过键名获取值

# http://127.0.0.1:8080/user/10086,响应:User ID: 10086
async def handler(request):
    # 获取路由参数({user_id})
    user_id = request.match_info.get("user_id", "0")
    # 直接通过[]获取(键不存在会抛KeyError,慎用)
    # user_id = request.match_info["user_id"]
    return web.Response(text=f"User ID: {user_id}")
app = web.Application()
app.add_routes([web.get("/user/{user_id}", handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.1.4、url

完整的请求 URL(含域名、路径、查询参数),支持拆解各部分

# 常用子属性:
* request.url.host: 域名      如:127.0.0.1
* request.url.port: 端口     如: 8080
* request.url.scheme: 协议   如: http/https
* request.url.query: 查询参数(同request.query)
# 访问 http://127.0.0.1:8080/url?a=1
async def handler(request):
    return web.json_response({
        "full_url": str(request.url),
        "host": request.url.host,
        "port": request.url.port,
        "scheme": request.url.scheme
    })
app = web.Application()
app.add_routes([web.get("/url", handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.1.5、method

获取大写的HTTP 请求方法

# http://127.0.0.1:8080/method,响应:Method: POST
async def handler(request):
    return web.Response(text=f"Method: {request.method}")
app = web.Application()
app.add_routes([
    web.post("/method", handler)
])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.1.6、headers

获取请求头信息,支持大小写不敏感查找

# http://127.0.0.1:8080/headers
async def handler(request):
    # 获取请求头(大小写不敏感)
    user_agent = request.headers.get("User-Agent", "未知")
    content_type = request.headers.get("Content-Type", "无")
    token = request.headers.get("X-Token", "无")

    return web.json_response({
        "User-Agent": user_agent,
        "Content-Type": content_type,
        "X-Token": token
    })
app = web.Application()
app.add_routes([web.get("/headers", handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.1.7、cookies

客户端携带的 Cookie,通过 key.value 获取值

# 客户端携带Cookie uid=1001;token=abc789 访问,响应:UID: 1001, Token: abc789
async def handler(request):
    # 获取Cookie
    uid = request.cookies.get("uid", "").value if "uid" in request.cookies else "无"
    token = request.cookies.get("token", "").value if "token" in request.cookies else "无"
    return web.Response(text=f"UID: {uid}, Token: {token}")
app = web.Application()
app.add_routes([web.get("/cookies", handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.1.8、remote

客户端的 IP 地址,用于记录访问日志、限制 IP 等

async def handler(request):
    return web.Response(text=f"Your IP: {request.remote}")
app = web.Application()
app.add_routes([web.get("/ip", handler)])
web.run_app(app, host="127.0.0.1", port=8080)

4.2.2、web.Request 核心方法 “必须加await”

4.2.2.1、json()

解析 JSON 格式的请求体(Content-Type 需为 application/json),失败抛 JSONDecodeError

# 测试:POST 提交 {"name":"张三","age":20},响应成功;提交非 JSON 数据,返回 400 错误
async def handler(request):
    try:
        # 解析JSON请求体
        data = await request.json()
        name = data.get("name")
        age = data.get("age")
        return web.json_response({"code": 200, "msg": "成功", "data": data})
    except Exception as e:
        return web.json_response({"code": 400, "msg": f"解析失败:{str(e)}"}, status=400)
app = web.Application()
app.add_routes([web.post("/json", handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.2.2、post()

解析表单格式的请求体
Content-Type 为 application/x-www-form-urlencoded 或 multipart/form-data

async def handler(request):
    # 解析表单数据
    form_data = await request.post()
    username = form_data.get("username")
    password = form_data.get("password")
    return web.json_response({"username": username, "password": password})
app = web.Application()
app.add_routes([web.post("/form", handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.2.3、files()

读取原始二进制请求体(通用方法,适合非 JSON / 表单的自定义格式,如 protobuf、二进制文件)

async def handler(request):
    # 读取原始二进制请求体
    raw_data = await request.read()
    return web.Response(
        text=f"原始数据长度:{len(raw_data)},内容:{raw_data.decode('utf-8')}",
        status=200
    )
app = web.Application()
app.add_routes([web.post("/raw", handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.2.4、content.read(n=-1)

流式读取请求体,适合大文件,避免一次性加载到内存
n 为读取字节数,-1表示读取全部

async def handler(request):
    # 流式读取请求体(每次1024字节)
    total_size = 0
    with open("large_file.bin", "wb") as f:
        while True:
            chunk = await request.content.read(1024)
            if not chunk:
                break
            total_size += len(chunk)
            f.write(chunk)
    return web.Response(text=f"大文件接收完成,总大小:{total_size} 字节")
app = web.Application(client_max_size=1024*1024*100)  # 允许最大100MB请求体
app.add_routes([web.post("/large_upload", handler)])
web.run_app(app, host="127.0.0.1", port=8080)

4.2.3、web.Response() 响应对象

aiohttp 服务端构建 HTTP 响应的基础类,所有路由处理函数最终都需要返回一个 Response 实例(或其子类如 json_response/FileResponse),它封装了响应状态码、响应头、响应体等所有响应相关的信息

参数名类型默认值核心含义 & 使用场景
textstrNone响应体为文本内容(自动编码为 bytes,默认 utf-8),适合返回纯文本、HTML 等
bodybytes/IOBaseNone响应体为二进制数据(优先级高于 text),适合返回自定义二进制、文件流等
statusint200HTTP 响应状态码(如 200 = 成功,400 = 参数错误,404 = 未找到,500 = 服务器错误)
reasonstrNone状态码描述(如 404 对应 “Not Found”,默认自动匹配状态码)
headersdict/CIMultiDictNone响应头(如 Content-Type、Set-Cookie、Cache-Control 等)
content_typestrNone响应体的 MIME 类型(如 text/html、application/json,优先级高于 headers 中的 Content-Type)
charsetstrutf-8文本响应的编码(仅对 text 参数生效)
compressint/boolFalse是否压缩响应体(True = 默认压缩级别,6 = 自定义级别,支持 gzip/deflate)
set_cookiestr/TupleNone快捷设置单个 Cookie(替代手动在 headers 中加 Set-Cookie)
set_cookiesIterable()批量设置多个 Cookie
headersdictNone自定义响应头(如跨域的 Access-Control-Allow-Origin)
chunkedboolFalse是否启用分块传输编码(Transfer-Encoding: chunked),适合大响应体(如流式返回)
timeoutfloatNone响应发送超时时间(秒)
4.2.3.1、基础文本响应

返回纯文本、HTML 等文本类型响应,核心用 text 参数,自动处理编码和 Content-Type

from aiohttp import web
async def text_response_handler(request):
    # 1. 纯文本响应(默认 Content-Type: text/plain; charset=utf-8)
    # return web.Response(text="Hello, aiohttp!")
    # 2. HTML 响应(指定 content_type 为 text/html)
    html_content = """
    <html>
        <head><title>Test Page</title></head>
        <body><h1>Hello, aiohttp!</h1></body>
    </html>
    """
    return web.Response(
        text=html_content,
        content_type="text/html",  # 关键:指定HTML类型
        status=200  # 状态码默认200
    )
app = web.Application()
app.add_routes([web.get("/text", text_response_handler)])
web.run_app(app, host="127.0.0.1", port=8080)
4.2.3.2、错误响应(4xx/5xx)

返回客户端 / 服务器错误响应,核心是设置 status 状态码和错误信息

async def error_response_handler(request):
    # 400 参数错误
    if "name" not in request.query:
        return web.Response(
            text=json.dumps({"code": 400, "msg": "name参数必传"}, ensure_ascii=False),
            content_type="application/json",
            status=400  # 关键:400状态码
        )
    # 500 服务器错误
    try:
        1 / 0  # 模拟异常
    except Exception as e:
        return web.Response(
            text=json.dumps({"code": 500, "msg": f"服务器错误:{str(e)}"}, ensure_ascii=False),
            content_type="application/json",
            status=500
        )
app.add_routes([web.get("/error", error_response_handler)])
4.2.3.3、流式响应(大文件 / 实时数据)

启用分块传输(chunked=True),流式返回数据(避免一次性加载大文件到内存)

async def stream_response_handler(request):
    # 构建流式响应(生成器返回分块数据)
    async def stream_generator():
        # 模拟逐行返回大文件内容
        with open("large_file.txt", "r", encoding="utf-8") as f:
            for line in f:
                yield line.encode("utf-8")  # 转为二进制分块
    return web.Response(
        body=stream_generator(),  # 流式响应体(生成器)
        content_type="text/plain",
        chunked=True  # 启用分块传输
    )
app.add_routes([web.get("/stream", stream_response_handler)])

4.2.4、web.json_response() 响应对象

参数名类型默认值核心含义 & 使用场景
datadict/list/str/int无(必填)要返回的 JSON 数据(支持任意可序列化的 Python 对象)
statusint200HTTP 响应状态码(如 200 = 成功,400 = 参数错误,500 = 服务器错误)
headersdict/CIMultiDictNone自定义响应头(如跨域头、Token、请求 ID 等)
content_typestrapplication/jsonJSON 响应的 MIME 类型(默认即可,无需修改)
charsetstrutf-8JSON 字符串的编码(确保中文 / 特殊字符正常显示)
dumpsCallablejson.dumps自定义 JSON 序列化函数(如用 ujson 提升性能)
**kwargs--继承 web.Response 的其他参数(如 reason/compress/set_cookie 等)
4.2.4.1、 基础 JSON 响应
from aiohttp import web
async def basic_json_handler(request):
    # 业务数据(任意可序列化的Python对象)
    response_data = {
        "code": 200,          # 自定义业务码
        "msg": "请求成功",     # 提示信息
        "data": {             # 业务数据体
            "user_id": 10086,
            "username": "张三",
            "age": 25,
            "tags": ["Python", "aiohttp"]
        }
    }
    
    # 构建JSON响应(核心:仅需传入data)
    return web.json_response(
        data=response_data,    # 必填:要序列化的JSON数据
        status=200,            # 可选:HTTP状态码,默认200
        headers={              # 可选:自定义响应头
            "X-Request-Id": "abc123456",  # 请求唯一标识
            "X-App-Version": "1.0.0"      # 应用版本
        }
    )
app = web.Application()
app.add_routes([web.get("/basic-json", basic_json_handler)])
if __name__ == "__main__":
    web.run_app(app, host="127.0.0.1", port=8080)
4.2.4.2、 错误 JSON 响应(4xx/5xx)
# 访问 http://127.0.0.1:8080/error-json(无 username 参数):返回 400 状态码 + 错误 JSON
# 访问 http://127.0.0.1:8080/error-json?username=张三:返回 500 状态码 + 异常信息
async def error_json_handler(request):
    # 模拟参数校验失败
    username = request.query.get("username")
    if not username:
        # 400 参数错误响应
        return web.json_response(
            data={
                "code": 40001,  # 自定义错误码(区分不同错误)
                "msg": "用户名不能为空",
                "data": None
            },
            status=400,  # HTTP状态码:400 Bad Request
            reason="Missing Required Parameter"  # 状态码描述(可选)
        )
    
    # 模拟服务器异常
    try:
        1 / 0  # 故意触发异常
    except Exception as e:
        # 500 服务器错误响应
        return web.json_response(
            data={
                "code": 50001,
                "msg": f"服务器内部错误:{str(e)}",
                "data": None
            },
            status=500  # HTTP状态码:500 Internal Server Error
        )
app.add_routes([web.get("/error-json", error_json_handler)])
4.2.4.2、 自定义 JSON 序列化函数
# 先安装:pip install ujson
import ujson
async def custom_dumps_handler(request):
    data = {"name": "李四", "score": 95.5}
    return web.json_response(
        data=data,
        dumps=ujson.dumps  # 自定义序列化函数(替换默认的json.dumps)
    )
app.add_routes([web.get("/custom-dumps", custom_dumps_handler)])

4.2.5、web.FileResponse() 响应对象

web.Response 的子类,专门用于高效返回文件(如图片、视频、文档、压缩包等)
关键特性:
1、自动推断 MIME 类型:根据文件后缀自动设置 Content-Type(如 .jpg → image/jpeg,.zip → application/zip);
2、断点续传:默认支持 Range 请求(如视频播放时拖拽进度条、大文件断点下载);
3、文件不存在处理:若 path 指向的文件不存在,自动抛出 web.HTTPNotFound 异常;
4、跨平台兼容:支持 Windows/macOS/Linux 的文件路径格式。

参数名类型默认值核心含义 & 使用场景
pathstr/Path无(必填)要返回的文件路径(绝对路径 / 相对路径均可)
statusint200HTTP 响应状态码(文件存在 = 200,不存在 = 404)
headersdict/CIMultiDictNone自定义响应头(如强制下载的 Content-Disposition)
content_typestrNone手动指定文件的 MIME 类型(默认自动推断)
chunk_sizeint262144 (256KB)流式传输的分块大小(可根据文件类型调整)
filenamestrNone下载时显示的文件名(覆盖原文件名)
last_modifieddatetimeNone手动指定文件最后修改时间(默认读取文件元数据)
cache_controlstrNone缓存控制头(如 max-age=3600,控制浏览器缓存)
allow_rangeboolTrue是否支持断点续传(Range 请求),默认开启
4.2.5.1、基础文件预览(图片 / 视频 / 文档)

返回文件供浏览器直接预览(如图片、HTML、PDF),无需下载

# 访问 http://127.0.0.1:8080/preview-image,浏览器直接显示图片
from aiohttp import web
from pathlib import Path
# 确保文件存在(test.jpg,可替换为自己的文件路径)
FILE_PATH = Path(__file__).parent / "test.jpg"
async def file_preview_handler(request):
    # 构建FileResponse(核心:仅需传入文件路径)
    return web.FileResponse(
        path=FILE_PATH,          # 必填:文件路径(Path对象/字符串均可)
        status=200,              # 可选:默认200
        cache_control="max-age=3600"  # 可选:浏览器缓存1小时
    )
app = web.Application()
app.add_routes([web.get("/preview-image", file_preview_handler)])
if __name__ == "__main__":
    web.run_app(app, host="127.0.0.1", port=8080)
4.2.5.2、文件强制下载(指定文件名)

让浏览器触发文件下载(而非预览),并自定义下载文件名

# 访问 http://127.0.0.1:8080/download-zip,浏览器自动下载文件,文件名显示为 我的压缩包.zip(而非原文件名 test.zip)
from aiohttp import web
from pathlib import Path
async def file_download_handler(request):
    # 原文件:test.zip,下载时显示为 "我的压缩包.zip"
    zip_path = Path(__file__).parent / "input" / "image_0.jpg"
    # 构建下载响应
    return web.FileResponse(
        path=zip_path,
        headers={
            # 强制下载(优先级高于浏览器默认预览行为)
            "Content-Disposition": 'attachment; filename="我的压缩包.zip"'
        }
    )
app = web.Application()
app.add_routes([web.get("/download-zip", file_download_handler)])
if __name__ == "__main__":
    web.run_app(app, host="127.0.0.1", port=8080)
4.2.5.3、大文件流式传输(支持断点续传)

返回超大文件(如 GB 级视频 / 压缩包),利用 FileResponse 的流式传输和断点续传特性
1、浏览器播放视频时,可拖拽进度条(断点续传)
2、下载大文件时,中断后可继续下载(无需重新开始)
3、内存占用始终为分块大小(1MB),不会加载整个文件到内存

async def large_file_handler(request):
    # 超大文件路径(如 2GB 的视频文件)
    large_file_path = Path(__file__).parent / "large_video.mp4"
    return web.FileResponse(
        path=large_file_path,
        chunk_size=1048576,  # 调整分块大小为 1MB(默认256KB,大文件可调大)
        allow_range=True,    # 开启断点续传(默认True,可省略)
        content_type="video/mp4"  # 手动指定MIME类型(确保视频播放器识别)
    )
app.add_routes([web.get("/large-video", large_file_handler)])
4.2.5.4、处理文件不存在的场景

主动捕获文件不存在的异常,返回自定义 JSON 错误响应

async def safe_file_handler(request):
    file_path = Path(__file__).parent / "non_exist_file.txt"
    try:
        # 尝试返回文件
        return web.FileResponse(path=file_path)
    except web.HTTPNotFound:
        # 文件不存在时,返回自定义JSON错误
        return web.json_response(
            data={
                "code": 40401,
                "msg": "文件不存在",
                "data": None
            },
            status=404
        )
app.add_routes([web.get("/safe-file", safe_file_handler)])
4.2.5.5、限制文件访问(权限校验)
async def auth_file_handler(request):
    # 1. 权限校验:获取请求头中的Token
    token = request.headers.get("X-Token")
    if token != "valid_token_123":
        # 无权限:返回403错误
        return web.json_response(
            data={"code": 40301, "msg": "无访问权限"},
            status=403
        )
    # 2. 权限通过:返回文件
    file_path = Path(__file__).parent / "secret_doc.pdf"
    return web.FileResponse(path=file_path)
app.add_routes([web.get("/auth-file", auth_file_handler)])
4.2.5.5、临时文件清理

若返回的是临时文件,建议在响应完成后清理

import aiofiles.os
async def temp_file_handler(request):
    temp_path = Path("temp_file.txt")
    resp = web.FileResponse(path=temp_path)
    # 响应完成后删除临时文件
    async def cleanup():
        await aiofiles.os.remove(temp_path)
    request.loop.create_task(cleanup())
    return resp

4.3、中间件管理(请求 / 响应拦截)

中间件是「请求到达处理函数前」和「响应返回客户端前」的拦截器,用于统一处理日志、权限、跨域等逻辑

4.3.1、middlewares

注册全局中间件,初始化时指定,或动态修改

# 定义中间件(格式:app → handler → middleware_handler)
async def logger_middleware(app, handler):
    async def middleware_handler(request):
        # 请求到达前:打印日志
        print(f"Request: {request.method} {request.path}")
        # 执行原处理函数
        response = await handler(request)
        # 响应返回前:打印状态码
        print(f"Response: {response.status}")
        return response
    return middleware_handler

# 初始化应用时注册中间件(可传多个,按顺序执行)
app = web.Application(middlewares=[logger_middleware])
app.add_routes([web.get("/hello", hello_handler)])
# 添加跨域中间件
async def cors_middleware(app, handler):
    async def middleware_handler(request):
        # 1. 处理OPTIONS预检请求:直接返回204,无需执行后续逻辑
        if request.method == "OPTIONS":
            response = web.Response(status=204)
        else:
            # 2. 非OPTIONS请求,执行原处理函数
            response = await handler(request)
        # 添加跨域头
        response.headers["Access-Control-Allow-Origin"] = "*"  # 允许所有域名,生产可指定具体域名
        response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"  # 允许上传的POST+预检OPTIONS
        response.headers["Access-Control-Allow-Headers"] = "Content-Type, Content-Length"  # 允许FormData上传的关键头
        response.headers["Access-Control-Max-Age"] = "3600"  # 预检请求缓存1小时,避免重复发送OPTIONS
        return response
    return middleware_handler

# 初始化应用时注册中间件(可传多个,按顺序执行)
app = web.Application(middlewares=[cors_middleware])
app.add_routes([web.get("/hello", hello_handler)])

五、客户端 + 服务端 完整案例

5.1、GET请求

客户端:

import asyncio
import aiohttp
# 全局超时配置
timeout = aiohttp.ClientTimeout(total=10)

async def demo_get(session):
    params = {"name": "张三", "age": 20}  # 查询参数:自动拼接为 ?name=张三&age=20
    # 单次请求头(覆盖全局)
    headers = {"Accept": "application/json"}
    try:
        # 2. 发起 GET 请求,await 等待响应
        async with session.get(
            url="http://127.0.0.1:8080/get",
            params=params,
            headers=headers,
            timeout=timeout
        ) as response:
            if response.status == 200:
                return await response.text()  # 获取文本响应,如 HTML/JSON 字符串
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")
async def main():
    # 1. 创建 ClientSession(异步上下文管理器),自动管理连接池
    async with aiohttp.ClientSession(
            headers={"User-Agent": "aiohttp-demo/1.0"},  # 全局请求头
            timeout=timeout
    ) as session:
        res = await demo_get(session)
        print("获取到服务器的返回结果:")
        print(res)
if __name__ == '__main__':
    asyncio.run(main())

服务端:

from aiohttp import web
# 1. 定义请求处理函数
async def handle(request):
    """
    使用web.request解析请求传递到服务端的参数
    """
    name = request.query.get('name', '未知用户')
    age = request.query.get('age', 0)
    print(f"接收到的参数为:{request.query}")
    return web.Response(
        text=f"请求用户为:{name}, 年龄是:{age}",
        status=200
    )
# 2. 创建应用实例
app = web.Application(
    client_max_size=2*1024*1024, # 最大客户端请求体大小为2MB
)
# 3. 注册路由
app.add_routes(
    [web.get('/get', handle)] # GET方法访问根路径时调用handle函数
)
if __name__ == '__main__':
    # 5. 启动服务
    web.run_app(
        app,
        host='127.0.0.1',  # 监听地址
        port=8080,        # 监听端口
        access_log=web.access_logger, # 访问日志记录器
    )

5.2、POST 请求 - 表单请求体

客户端:

import asyncio
import json
import aiohttp

# 全局超时配置
timeout = aiohttp.ClientTimeout(total=10)

async def demo_post(session):
    form_data = {"gender": "male", "city": "Beijing"}
    # 单次请求头
    headers = {"Accept": "application/x-www-form-urlencoded"}

    try:
        # 2. 发起 post 请求,await 等待响应
        async with session.post(
            url="http://127.0.0.1:8080/post",
            data=form_data,
            headers=headers,
            timeout=timeout
        ) as response:
            if response.status == 200: # HTTP 状态码判断
                return await response.json()  # 获取服务端返回的 JSON 响应数据
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")

async def main():
    # 1. 创建 ClientSession(异步上下文管理器),自动管理连接池
    async with aiohttp.ClientSession(
            headers={"User-Agent": "aiohttp-demo/1.0"},  # 全局请求头
            timeout=timeout
    ) as session:
        result = await demo_post(session)
        print("获取到服务器的返回结果:")
        print(json.dumps(result, indent=2, ensure_ascii=False))

if __name__ == '__main__':
    asyncio.run(main())

服务端:

from aiohttp import web
# 1. 定义请求处理函数
async def handle(request):
    """
    使用web.request解析请求传递到服务端的参数
    案例中客户端发送了 普通表单数据
    """
    # print(request.method)
    # print(request.headers)
    form_data = await request.post()
    gender = form_data.get("gender")
    city = form_data.get("city")

    print(f"异步服务端接收到的参数: gender={gender}, city={city}")

    response_data = {
        "code": "200",
        "msg": "请求成功",
        "data": {
            "gender": gender,
            "city": city
        }
    }
    # 使用json_response响应客户端请求
    return web.json_response(
        data=response_data, # 响应数据
        status=200
    )
# 2. 创建应用实例
app = web.Application(
    debug=False, # 启用调试模式
    client_max_size=2*1024*1024, # 最大客户端请求体大小为2MB
)
# 3. 注册路由
app.add_routes(
    [web.post('/post', handle)] # GET方法访问根路径时调用handle函数
)
if __name__ == '__main__':
    # 5. 启动服务
    web.run_app(
        app,
        host='127.0.0.1',  # 监听地址
        port=8080,        # 监听端口
        access_log=web.access_logger, # 访问日志记录器
    )

5.3、POST 请求 - form-data请求体

import asyncio
import json
import aiohttp

# 全局超时配置
timeout = aiohttp.ClientTimeout(total=10)

async def demo_post(session):
    multipart_data = aiohttp.FormData()
    multipart_data.add_field("username", "user")
    multipart_data.add_field("password", "pass123456")

    # 单次请求头
    headers = {"Content-Type": "application/x-www-form-urlencoded"}

    try:
        # 2. 发起 post 请求,await 等待响应
        async with session.post(
            url="http://127.0.0.1:8080/post",
            data=multipart_data,
            headers=headers,
            timeout=timeout
        ) as response:
            if response.status == 200: # HTTP 状态码判断
                return await response.json()  # 获取服务端返回的 JSON 响应数据
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")

async def main():
    # 1. 创建 ClientSession(异步上下文管理器),自动管理连接池
    async with aiohttp.ClientSession(
            headers={"User-Agent": "aiohttp-demo/1.0"},  # 全局请求头
            timeout=timeout
    ) as session:
        result = await demo_post(session)
        print("获取到服务器的返回结果:")
        print(json.dumps(result, indent=2, ensure_ascii=False))
if __name__ == '__main__':
    asyncio.run(main())
from aiohttp import web
# 1. 定义请求处理函数
async def handle(request):
    """
    使用web.request解析请求传递到服务端的参数
    案例中客户端发送了 form-data表单数据
    """
    form_data = await request.post()
    username = form_data.get("username")
    password = form_data.get("password")

    print(f"异步服务端接收到的参数: gender={username}, password={password}")

    response_data = {
        "code": "200",
        "msg": "请求成功",
        "data": {
            "username": username,
            "password": password
        }
    }
    # 使用json_response响应客户端请求
    return web.json_response(
        data=response_data, # 响应数据
        status=200
    )

# 2. 创建应用实例
app = web.Application(
    debug=False, # 启用调试模式
    client_max_size=2*1024*1024, # 最大客户端请求体大小为2MB
)
# 3. 注册路由
app.add_routes(
    [web.post('/post', handle)] # GET方法访问根路径时调用handle函数
)
if __name__ == '__main__':
    # 5. 启动服务
    web.run_app(
        app,
        host='127.0.0.1',  # 监听地址
        port=8080,        # 监听端口
        access_log=web.access_logger, # 访问日志记录器
    )

5.4、POST 请求 -json请求体

客户端:

import asyncio
import json
import aiohttp
# 全局超时配置
timeout = aiohttp.ClientTimeout(total=10)
async def demo_post(session):
    json_data = {"name": "张三", "age": 20} 

    # 单次请求头
    headers = {"Accept": "application/json"}

    try:
        # 2. 发起 GET 请求,await 等待响应
        async with session.post(
            url="http://127.0.0.1:8080/post",
            json=json_data,
            headers=headers,
            timeout=timeout
        ) as response:
            if response.status == 200: # HTTP 状态码判断
                return await response.json()  # 获取服务端返回的 JSON 响应数据
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")

async def main():
    # 1. 创建 ClientSession(异步上下文管理器),自动管理连接池
    async with aiohttp.ClientSession(
            headers={"User-Agent": "aiohttp-demo/1.0"},  # 全局请求头
            timeout=timeout
    ) as session:
        result = await demo_post(session)
        print("获取到服务器的返回结果:")
        print(json.dumps(result, indent=2, ensure_ascii=False))
if __name__ == '__main__':
    asyncio.run(main())

服务端:

from aiohttp import web
# 1. 定义请求处理函数
async def handle(request):
    """
    使用web.request解析请求传递到服务端的参数
    案例中客户端发送了post请求
    """
    data = await request.json()
    name = data.get('name')
    age = data.get('age')
    print(f"异步服务端接收到的参数: name={name}, age={age}")

    response_data = {
        "code": "200",
        "msg": "请求成功",
        "data": {
            "username": name,
            "user_age": age
        }
    }
    # 使用json_response响应客户端请求
    return web.json_response(
        data=response_data, # 响应数据
        status=200
    )
# 2. 创建应用实例
app = web.Application(
    debug=False, # 启用调试模式
    client_max_size=2*1024*1024, # 最大客户端请求体大小为2MB
)
# 3. 注册路由
app.add_routes(
    [web.get('/post', handle)] # GET方法访问根路径时调用handle函数
)
if __name__ == '__main__':
    # 5. 启动服务
    web.run_app(
        app,
        host='127.0.0.1',  # 监听地址
        port=8080,        # 监听端口
        access_log=web.access_logger, # 访问日志记录器
    )

5.5、文件上传、下载、预览

5.5.1、python脚本上传

import asyncio
import json
import os.path
import aiohttp

# 全局超时配置
timeout = aiohttp.ClientTimeout(total=10)

file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "py.png")

async def demo_post(session):
    multipart_data = aiohttp.FormData()
    # 上传文件(本地文件路径)
    multipart_data.add_field(
        "file_field",  # 表单 name
        open(file_path, "rb"),  # 二进制模式打开
        filename=os.path.split(file_path)[-1],  # 文件名
        content_type="text/plain; charset=utf-8"  # MIME类型
    )

    try:
        # 2. 发起 post 请求,await 等待响应
        async with session.post(
            url="http://127.0.0.1:8080/upload",
            data=multipart_data,
            timeout=timeout
        ) as response:
            if response.status == 200:
                return await response.json()  # 获取服务端返回的 JSON 响应数据
    except aiohttp.ClientError as e:
        raise aiohttp.ClientError(f"Error fetching {response.url}: {str(e)}")

async def main():
    # 1. 创建 ClientSession(异步上下文管理器),自动管理连接池
    async with aiohttp.ClientSession(
            headers={"User-Agent": "aiohttp-demo/1.0"},  # 全局请求头
            timeout=timeout
    ) as session:
        result = await demo_post(session)
        print("获取到服务器的返回结果:")
        print(json.dumps(result, indent=2, ensure_ascii=False))
if __name__ == '__main__':
    asyncio.run(main())

5.5.2、HTML 网页上传

使用html网页版上传,要加跨域中间件

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>文件上传示例</title>
    <style>
        .container { max-width: 600px; margin: 50px auto; padding: 20px; }
        .upload-result { margin-top: 20px; padding: 10px; border: 1px solid #ccc; }
        a { color: #0066cc; text-decoration: none; }
        a:hover { text-decoration: underline; }
        .error { color: red; }
        .success { color: green; }
    </style>
</head>
<body>
    <div class="container">
        <h1>文件/图片上传</h1>
        <form id="uploadForm">
            <input type="file" id="fileInput" accept="image/*, .txt, .pdf" required>
            <button type="submit">上传</button>
        </form>
        <div class="upload-result" id="result" style="display: none;"></div>
    </div>

    <script>
        const form = document.getElementById("uploadForm");
        const resultDiv = document.getElementById("result");
        const fileInput = document.getElementById("fileInput");

        form.onsubmit = async (e) => {
            e.preventDefault();
            const file = fileInput.files[0];
            if (!file) {
                showResult("请选择要上传的文件!", "error");
                return;
            }

            const formData = new FormData();
            formData.append("file_field", file); // 无需任何编码,FormData自动处理

            try {
                const resp = await fetch("http://127.0.0.1:8080/upload", {
                    method: "POST",
                    body: formData // ✅ 绝对不要手动设置headers!浏览器自动生成
                });

                // ✅ 关键:处理非200的响应状态(如预检失败、404、500)
                if (!resp.ok) {
                    const errText = await resp.text() || `请求失败,状态码:${resp.status}`;
                    showResult(`❌ ${errText}`, "error");
                    return;
                }

                // 解析响应(确保响应是合法JSON,避免解析失败报undefined)
                const result = await resp.json();
                if (result.code === 200) {
                    showResult(`
                        <p class="success">✅ 上传成功!</p>
                        <p>文件名:${result.data.filename}</p>
                        <p>预览:<a href="${result.data.preview_url}" target="_blank">点击预览</a></p>
                        <p>下载:<a href="${result.data.download_url}" target="_blank">点击下载</a></p>
                    `);
                } else {
                    showResult(`❌ ${result.msg}`, "error");
                }
            } catch (err) {
                // ✅ 捕获网络错误、JSON解析错误等所有异常,避免undefined
                showResult(`❌ 上传失败:${err.message || "未知错误"}`, "error");
                console.error("上传异常:", err); // 控制台打印详细错误,方便排查
            }
        };

        // 封装结果展示函数,统一处理显示
        function showResult(html, className = "") {
            resultDiv.style.display = "block";
            resultDiv.innerHTML = html;
            if (className) {
                resultDiv.className = `upload-result ${className}`;
            } else {
                resultDiv.className = "upload-result";
            }
        }
    </script>
</body>
</html>

5.5.5、服务端

from aiohttp import web
from pathlib import Path
import urllib.parse
import mimetypes  # 用于精准推断MIME类型

UPLOAD_DIR = Path(__file__).parent / "uploads"
UPLOAD_DIR.mkdir(exist_ok=True)

# 定义请求处理函数
"""
使用web.request解析请求传递到服务端的参数
文件上传/预览/下载
"""
# 1. 文件/图片上传
async def upload_handler(request):
    # 解析上传的文件(Content-Type: multipart/form-data)
    data  = await request.post()
    # 获取上传的文件对象,前端表单的 name 属性需为 "file_field"
    file_data = data.get("file_field") # form-data对象中的file对象

    if not file_data:
        return web.json_response({"error": "No file uploaded"}, status=400)

    # 保存文件到本地 uploads 目录
    file_path = UPLOAD_DIR/file_data.filename
    with open(file_path, 'wb') as f:
        f.write(file_data.file.read()) # 用form-data对象中的file对象读取文件二进制并保存
        # 强制刷新缓冲区,确保二进制数据完整写入(避免大文件截断)
        f.flush()

    # 编码文件名,返回预览/下载URL
    encoded_filename = urllib.parse.quote(file_data.filename)

    return web.json_response({
        "code": 200,
        "msg": "上传成功",
        "data": {
            "filename": file_data.filename,
            "preview_url": f"http://127.0.0.1:8080/preview/{encoded_filename}",
            "download_url": f"http://127.0.0.1:8080/download/{encoded_filename}"
        }
    })

# 2. 文件/图片预览
async def preview_handler(request):
    filename = request.match_info.get('filename')

    try:
        filename = urllib.parse.unquote(filename)
    except Exception as e:
        return web.json_response({"code": 400, "msg": "文件名格式错误"}, status=400)

    file_path = UPLOAD_DIR / filename

    # 检查文件是否存在
    if not file_path.exists():
        return web.json_response({"code": 404, "msg": "文件不存在"}, status=404)

    # 推断文件MIME类型(解决aiohttp自动推断错误的问题)
    mime_type, _ = mimetypes.guess_type(file_path)
    # 兜底MIME类型:二进制流(避免推断失败)
    if not mime_type:
        mime_type = "application/octet-stream"

    resp = web.FileResponse(
        path=file_path
    )

    # 文本文件:在响应头指定编码为utf-8(解决txt/csv中文乱码)
    if mime_type.startswith("text/"):
        resp.headers["Content-Type"] = f"{mime_type}; charset=utf-8"
        resp.headers["Charset"] = "utf-8"
    return resp

# 3. 下载文件/图片(强制下载)
async def download_handler(request):
    filename = request.match_info.get('filename')
    try:
        filename = urllib.parse.unquote(filename)
    except Exception as e:
        return web.json_response({"code": 400, "msg": "文件名格式错误"}, status=400)

    file_path = UPLOAD_DIR / filename

    # 步骤2:检查文件是否存在
    if not file_path.exists():
        return web.json_response({"code": 404, "msg": "文件不存在"}, status=404)

    # 步骤3:构造下载响应头(中文文件名做 URL 编码)
    # Content-Disposition 中的中文必须编码,否则浏览器显示乱码
    download_name = urllib.parse.quote(filename)
    headers = {
        "Content-Disposition": f'attachment; filename="{download_name}"; filename*=UTF-8\'\'{download_name}'
    }

    # 步骤4:返回文件(强制下载,文件名正常显示)
    return web.FileResponse(
        path=file_path,
        headers=headers
    )

# 注册路由 + 启动服务
def main():
    # 2. 创建应用实例
    app = web.Application(
        debug=False,  # 启用调试模式
        client_max_size=10 * 1024 * 1024,  # 允许最大10MB文件上传
    )
    app.add_routes(
        [
            web.post('/upload', upload_handler),  # 上传接口
            web.get("/preview/{filename}", preview_handler),  # 预览接口
            web.get("/download/{filename}", download_handler)  # 下载接口
        ]
)
    web.run_app(
        app,
        host='127.0.0.1',  # 监听地址
        port=8080,        # 监听端口
        access_log=web.access_logger, # 访问日志记录器
    )
if __name__ == '__main__':
    main()

六、WebSocket 异步通信(客户端 + 服务器)

服务器端:

from aiohttp import web
async def websocket_handler(request):
    # 升级为WebSocket连接
    ws = web.WebSocketResponse()
    await ws.prepare(request)
    # 监听消息
    async for msg in ws:
        if msg.type == web.WSMsgType.TEXT:
            # 回复消息
            print(f"服务器收到消息:{msg.data}")
            await ws.send_str(f"thanks, hava a good day!")
            if msg.data == "close":
                await ws.close()
        elif msg.type == web.WSMsgType.ERROR:
            print(f"WebSocket错误:{ws.exception()}")

    print("WebSocket连接关闭")
    return ws
app = web.Application()
app.add_routes([web.get("/ws", websocket_handler)])
web.run_app(app, host="127.0.0.1", port=8080)

客户端:

import aiohttp
import asyncio
async def websocket_client():
    async with aiohttp.ClientSession() as session:
        # 连接WebSocket服务器
        async with session.ws_connect("http://127.0.0.1:8080/ws") as ws:
            # 发送消息
            await ws.send_str("Hello, WebSocket!")
            # 接收回复
            async for msg in ws:
                if msg.type == aiohttp.WSMsgType.TEXT:
                    print(f"客户端收到:{msg.data}")
                    if msg.data == "服务器收到:close":
                        await ws.close()
                        break
                elif msg.type == aiohttp.WSMsgType.ERROR:
                    break
asyncio.run(websocket_client())
python 基于aiohttp的异步爬虫实战 接下来我们会详细介绍aiohttp库的用法和爬取实战。aiohttp 是一个支持异步请求的库,它和 asyncio 配合使用,可以使我们非常方便地实现异步请求操作。asyncio模块,其内部实现了对TCP、UDP、SSL协议的异步操作,但是对于HTTP请求,就需要aiohttp实现了。aiohttp分为两部分,一部分是Client,一部分是Server。下面来说说aiohttp客户端部分的用法。以上就是借助协程async和异步aiohttp两个主要模块完成异步爬虫的内容, 阅读详情

相关推荐

Python3 爬虫教程 - aiohttp 的基本使用

前面介绍的 asyncio 模块内部实现了对 TCP、UDP、SSL 协议的异步操作,但是对于 HTTP 请求来说,我们就需要用到 aiohttp 来实现了。aiohttp 是一个基于 asyncio 的异步 HTTP 网络模块,它既提供了服务端,又提供了客户端。其中我们用服务端可以搭建一个支持异步处理的服务器,就是用来处理请求并返回响应的,类似于 Django、Flask、Tornado 等一些 Web 服务器。而客户端可以用来发起请求,类似于使用 requests 发起一个 HTTP 请求然后获得响应,

xiaoganaiuk的博客 3486

Python aiohttp 完全指南:快速入门

aiohttp 是一个基于异步 I/O 的 Web 框架,专注于提供高性能、低开销的异步 Web 服务。它允许我们同时处理大量并发请求,而不会阻塞程序执行。aiohttp 使用 Python 的 async/await 语法来实现异步编程,这使得编写异步代码更加直观和简洁。在传统的同步编程中,每个任务都是按照顺序依次执行的。如果一个任务需要等待一些耗时的操作(如网络请求或文件读取),那么整个程序将会被阻塞,导致其他任务无法执行。

LiamHong_的博客 9655

Python异步HTTP编程:aiohttp从入门到高并发爬虫实战

异步编程是一种事件驱动的编程范式,其核心原理是在等待I/O操作(如网络请求、文件读写)时,将CPU控制权让出给其他任务,从而实现单线程内的高并发处理。这种非阻塞I/O模型能显著提升网络应用的吞吐量和资源利用率,尤其适用于需要同时处理大量连接的场景。在Python生态中,asyncio库提供了原生的异步支持,而aiohttp则是基于asyncio构建的异步HTTP客户端/服务器框架。aiohttp的技术价值在于它允许开发者用协程(Coroutine)和事件循环(Event Loop)轻松管理成千上万个并发连接

weixin_33709590的博客 678

aiohttp从入门到精通

异步编程是一种编写并发代码的方法,它能够提高程序的性能和响应速度。在传统的同步编程模型中,程序会等待一个函数或任务完成才能继续执行下一个任务,这可能会导致长时间的等待和阻塞。而在异步编程模型中,程序可以同时处理多个任务,并且不会阻塞主线程。异步编程通常基于回调、事件循环和协程来实现。回调是指当一个函数完成时,调用另一个函数来处理结果。事件循环是一种机制,通过在一个无限循环中不断地监听和处理事件,来实现异步操作和并发处理。

所寫即所思|一个阿里质量人对测试的所感所悟。 4607

python爬虫之看懂aiohttp异步爬虫

接下来我们会详细介绍aiohttp库的用法和爬取实战。aiohttp 是一个支持异步请求的库,它和 asyncio 配合使用,可以使我们非常方便地实现异步请求操作。asyncio模块,其内部实现了对TCP、UDP、SSL协议的异步操作,但是对于HTTP请求,就需要aiohttp实现了。aiohttp分为两部分,一部分是Client,一部分是Server。下面来说说aiohttp客户端部分的用法。

weixin_68056852的博客 1735

python-25-协程异步aiohttp爬取数据

异步协程aiohttp

qq_20466211的博客 2226

【Python】aiohttp库:基于 asyncio 的异步 HTTP 客户端和服务器框架

aiohttp(Asynchronous Input/Output HTTP)是 Python 的一个开源异步 HTTP 框架,构建于 asyncio 库之上,采用 async/await 语法,支持 HTTP 客户端和服务器功能。核心特点:异步处理:利用 asyncio 实现非阻塞 I/O,适合高并发场景。双向功能:支持 HTTP 客户端(发送请求)和服务器(处理请求)。丰富功能:包括 WebSockets、路由、中间件、JSON 处理和流式传输。高性能:研究表明,aiohttp 在并发请求下优于同步库(

彬彬侠的博客 5327

Python异步HTTP库aiohttp的全解析及应用

本文还有配套的精品资源,点击获取 简介:aiohttp是一个基于Python的asyncio框架的HTTP客户端和服务器异步库。它为开发者提供了一套易于使用且功能强大的工具,用于编写高效率的并发网络代码。本详解介绍aiohttp的核心组件,包括客户端、服务器和Web套件。通过对比requests库,阐明了aiohttp在并发场景下的优势,并提供了安装和使用指导。同时,概述了...

weixin_35755823的博客 7218

Python aiohttp 全面指南:异步HTTP客户端/服务器框架

aiohttp是一个基于 Python asyncio 的异步 HTTP 客户端/服务器框架,专为高性能网络编程设计。异步 HTTP 客户端(类似异步版 requests)异步 HTTP 服务器(类似异步版 Flask/Django)完整的 WebSocket 支持高效的连接池管理高效客户端:用于高性能爬虫、API调用轻量级服务器:构建高性能Web服务和APIWebSocket支持:实现实时双向通信连接池管理:优化资源利用率。

记录点滴 1527

精通Python异步网络编程:aiohttp实战指南

asyncio库是Python中用于编写单线程并发代码的一个库,它直接集成在Python标准库中,自Python 3.4开始引入。asyncio的设计初衷是为了满足高性能网络和Web服务器的需求,通过提供一个运行单线程调度器的事件循环(event loop)来实现。asyncio采用了协作式多任务的模型,在此模型下,每个任务(协程)主动放弃控制权,让事件循环调度其他任务,以此达到异步执行的效果。async关键字用于定义一个异步函数,它告诉Python解释器,这个函数将在协程中运行,能够执行异步操作。

weixin_35706255的博客 1431

基于IC617下Layout XL的使用

基于IC617下Layout XL的使用

weixin_43816759的博客 1万+

python模块之aioHttp 异步请求

aiohttp 是一个基于异步的 Python HTTP 客户端/服务器框架,它允许你编写高性能的异步网络应用程序。它建立在 Python 的协程和异步 I/O 模型上,并利用了 Python 3.5+ 中引入的 asyncio 库。异步支持:aiohttp 基于 asyncio 库,充分利用了 Python 的协程和异步 I/O 模型,可以处理大量并发请求而不阻塞其他任务。这使得它非常适合编写高性能的网络应用程序。

老衲爱洗头的博客 5298

ABB AC500 系列 PLC 与上位机iFix 的通讯配置

在 软件中,有 3 种方式可以进入 IP config tool 的配置界面 双击 IP_settings,点击 IP config tool,即可进入 IP config tool 界面点击菜单栏的 Tool→IP config如果单独安装了 ABB IP Configration,则可以通过开始→程序→ABB→ABB IPConfigration 启动 IP 配置工具启动 IP config tool 后,即可进入 IP 配置界面。

xdpcxq的专栏 4037

aiohttp-demos完全指南:从入门到精通的异步Web开发实战教程

aiohttp-demos是基于aiohttp框架的异步Web开发示例集合,包含多个实用项目,帮助开发者快速掌握异步编程技术。本教程将带你从环境搭建到实际项目运行,全面了解aiohttp的核心功能和应用场景。 ## 为什么选择aiohttp进行异步Web开发? 在当今高并发的网络环境中,异步编程已成为提升应用性能的关键技术。aiohttp作为Python生态中成熟的异步Web框架,具有以下优势

gitblog_00442的博客 1093

C语言新手必练:10个实战小项目带你快速上手(附完整代码)

本文为C语言初学者精选10个实战项目,从基础的温度转换器到高级的迷宫求解算法,涵盖输入输出、条件判断、循环控制、数组操作等核心知识点。每个项目配有完整代码和详细解析,帮助新手通过实践掌握编程思维,特别适合刚学完语法但缺乏实战经验的学习者。

weixin_29208077的博客 769

YOLOv5算法改进(21)— 添加CA注意力机制 + 更换Neck网络之BiFPN + 更换损失函数之EIoU

本节课作者就结合论文对YOLOv5进行组合改进(添加CA注意力机制+更改Neck网络之BiFPN+更换损失函数之EIoU),小白必学!

小哥谈 2822

Python aiohttp 使用指南:快速入门教程

aiohttp 是一个基于异步 I/O 的 Web 框架,专注于提供高性能、低开销的异步 Web 服务。它允许我们同时处理大量并发请求,而不会阻塞程序执行。aiohttp 使用 Python 的 async/await 语法来实现异步编程,这使得编写异步代码更加直观和简洁。在传统的同步编程中,每个任务都是按照顺序依次执行的。如果一个任务需要等待一些耗时的操作(如网络请求或文件读取),那么整个程序将会被阻塞,导致其他任务无法执行。

aobulaien001的博客 1871

Vue + ECharts:从零构建智慧农业数据监控大屏(附完整源码)」 「超实用!一步步实现智慧农业大屏,完整代码+讲解」 「前端新手友好:Vue + ECharts 打造炫酷农业数据大屏」 「

Vue + ECharts:从零构建智慧农业数据监控大屏(附完整源码)」 「超实用!一步步实现智慧农业大屏,完整代码+讲解」 「前端新手友好:Vue + ECharts 打造炫酷农业数据大屏」 「前端可视化项目实战:智慧农业监控大屏开发详解」 「农作物可视化监控:用 Vue 和 ECharts 构建大屏应用」 「从零开始开发一个智慧农业数据大屏(代码+图表展示)」 「前端新手也能实现!智慧农业监控大屏开发教程」 「用 Vue 和 ECharts 搭建动态数据大屏:详细图文教程」 「学习前端大屏开发:

sky10100100的博客 1537

python3 异步 asyncio aiohttp aiohttp-requests aiofiles 使用

asyncio asyncio 是用来编写并发代码的库,使用async/await语法。 asyncio 被用作多个提供高性能 Python 异步框架的基础,包括网络和网站服务,数据库连接库,分布式任务队列等等。 asyncio 往往是构建 IO 密集型和高层级结构化网络代码的最佳选择。 aiohttp aiohttp是一个为Python提供异步HTTP 客户端/服务端编程,基于asyncio(Python用于支持异步编程的标准库)的异步库。在爬虫时提供异步网络请求,而常用到的req...

whatday的专栏 3393

【Unity3D框架】GameFramework框架完全教程《全网最全》-专栏文章目录

最近准备学习框架,了解到GameFramework框架是国产Unity3D框架,准备学习一下,但是奈何全网关于这个框架的教程实在太少。为了让自己学习方便一些,也为了让后来者乘凉。将框架以自己的理解整理目录,并且补全教程。觉得教程有帮助别忘了一键三连哦。

CSDN博客专家,Unity3D高级软件工程师,《Unity 3D从入门到实战》作者。 2万+

vue2/3 - H5 移动端网页中调用手机摄像头扫描二维码 / 条形码功能,在h5手机网页里调起相机摄像头进行二维码或条形码扫描效果详细教程(详细示例组件源码,一键复制开箱即用!)

vue扫描二维码条形码,vue网页调用摄像头扫二维码条形码,vue移动端如何实现调用相机扫描二维码,vue移动端扫一扫功能,vue怎么做移动端h5扫一扫功能,vue识别二维码和条形码,vue实现点击按钮调用摄像头扫码,vue调用本地摄像头实现拍照功能,Vue中如何进行二维码生成与扫描,如何实现vue在手机浏览器中调用摄像头实现扫描二维码功能,VUE项目移动端H5调用摄像头扫码,vue项目中实现H5调取摄像头,网页调用摄像头实现二维码扫描功能,vue调用手机摄像头,vue在调用摄像头扫码,vue自定义打开摄像

✅ 精选推荐文章 1万+

aiohttp 异步http请求-1.快速入门 get 请求示例

前言 在 python 的众多 http 请求库中,大家最熟悉的就是 requests 库了,requests 库上手非常容易,适合入门学习。 如果平常工作中对发请求不追求效率和并发的情况下,requests 完全可以满足大部分需求。但是想发大量的请求,比如几万个请求的时候,可能需要等待几个小时,requests 库就不能满足需求了。 初学者建议先学习requests 库,熟练掌握requests 库才能更好的学习 aiohttp 异步框架。 同步与异步 requests只能发送同步请求,aiohttp只能

qq_27371025的博客 4059

【C2000系列DSP的Bootloader详解】如何打包源文件为库文件且加密仅授权使用,一步步教你如何实现

如果想要在 TI 的 CCS 开发环境中为 C2000 系列 MCU 生成 lib 库文件,并对这些库文件进行加密保护,确保只有授权方(持有密钥)才能调用和使用,以此保护源代码的知识产权。

昔时扬尘处 181

第11课:asyncio|异步HTTP请求【aiohttp 从入门到精通】

aiohttp 异步HTTP客户端指南 摘要 本文介绍了aiohttp异步HTTP客户端的使用方法,重点讲解了ClientSession的设计原理和最佳实践。主要内容包括: ClientSession核心功能:连接池管理、Cookie持久化、统一配置请求参数,强调必须复用Session以提高性能 响应处理机制:详细解释了ClientResponse的工作流程,指出响应头与响应体的异步获取过程 HTTP请求示例: GET请求的基本用法 POST请求支持JSON、表单数据和文件上传等多种格式 演示了如何正确处理

Java 后端|Python|AI 实战博主。专注微服务、分布式、大模型应用开发,输出可落地项目、源码剖析与面试总结。用通俗文字拆解技术难点,记录开发路上的学习与踩坑,期待和大家一起交流成长。 531

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

aiohttp:Python 异步 HTTP 编程的首选框架

aiohttp是Python生态中成熟的异步HTTP框架,提供客户端和服务器的全栈解决方案。它凭借成熟稳定、API设计友好和优异性能成为异步HTTP编程的首选,支持高并发爬虫、Web服务和实时应用等场景。该框架具有完善的WebSocket支持,易于上手(类似requests和Flask的API风格),并拥有活跃社区和丰富生态系统。尽管异步编程存在调试复杂等固有挑战,但aiohttp仍是构建高性能异步应用的理想选择,特别适合处理大量并发请求和实时通信需求。建议使用者先掌握asyncio基础,并参考详细官方文档

shenlongcode387的博客 374

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

23. 异步HTTP请求与aiohttp模块

在上一节中,我们发现time.sleep()不是异步的,导致我们的异步函数无法按异步执行。实际上,requests模块的网络请求函数,get()、post()等也都不是异步的。所以我们想用它们做异步爬虫与网络请求时,就必须使用异步的HTTP请求,从而让异步函数正常工作。那么在Python中如何实现异步HTTP请求呢?这就需要用到我们本节要介绍的新模块——aiohttpasyncio可以实现单线程并发IO操作。如果仅用在客户端,发挥的威力不大。如果把asyncio。

Vec_Kun的博客 517

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

上一篇: python3从入门到精通(五): python协程之asyncio模块(异步IO)(一)
下一篇: Git版本管理 使用详解
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一位不知名民工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址