python network_error ai_generated true

aiohttp 客户端 HTTP 版本错误:无效的 HTTP 版本:HTTP/2

aiohttp.client_exceptions.ClientHttpVersionError: Invalid HTTP version: HTTP/2

ID: python/aiohttp-client-connector-http-version-error

其他格式: JSON · Markdown 中文 · English
80%修复率
82%置信度
0证据数
2025-09-10首次发现

版本兼容性

版本状态引入弃用备注
3.10 active
3.11 active

根因分析

服务器使用 HTTP/2,但 aiohttp 配置为仅使用 HTTP/1.1,反之亦然。

English

The server uses HTTP/2, but aiohttp is configured to use HTTP/1.1 only, or vice versa.

generic

解决方案

  1. 90% 成功率 Enable HTTP/2 support with aiohttp
    import aiohttp
    async with aiohttp.ClientSession(http2=True) as session:
        async with session.get(url) as resp:
  2. 85% 成功率 Use a custom connector with HTTP/2 enabled
    connector = aiohttp.TCPConnector(force_close=True, http2=True)
    async with aiohttp.ClientSession(connector=connector) as session:

无效尝试

常见但无效的做法:

  1. Disabling HTTP/2 support entirely 40% 失败

    Some servers require HTTP/2 for optimal performance or functionality.

  2. Manually setting the HTTP version in headers 60% 失败

    The HTTP version is negotiated at the protocol level, not via headers.