You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Ubuntu至22.04后Python HTTPS请求因OpenSSL 3变慢求解决方案

解决Ubuntu 22.04下OpenSSL 3.x导致Python HTTPS请求变慢的问题

问题概述

Ubuntu 20.04升级到22.04后,Python脚本的HTTPS请求耗时从约0.7秒飙升至2秒,确认是OpenSSL 3.x版本的默认配置导致性能下降,无需重新编译Python,可通过以下方案适配优化:

1. 修改OpenSSL全局配置平衡安全与性能

编辑/etc/ssl/openssl.cnf,添加或更新以下配置段:

[default_conf]
ssl_conf = ssl_sect

[ssl_sect]
system_default = system_default_sect

[system_default_sect]
MinProtocol = TLSv1.2
CipherString = DEFAULT:@SECLEVEL=1
  • 说明:将SSL安全级别从默认的2降至1,减少不必要的加密算法校验开销,同时指定最低TLS版本为1.2,兼顾基础安全与性能。修改后无需重启系统,新建的Python进程会自动读取新配置。

2. 在requests中使用自定义优化的SSL上下文

无需全局修改配置,仅在脚本内为requests设置优化后的SSL上下文:

import requests
import threading
import time
import ssl
from urllib3.poolmanager import PoolManager

class OptimizedSSLAdapter(requests.adapters.HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        # 创建自定义SSL上下文
        ctx = ssl.create_default_context()
        ctx.set_ciphers('DEFAULT:@SECLEVEL=1')
        ctx.minimum_version = ssl.TLSVersion.TLSv1_2
        # 初始化连接池时使用该上下文
        self.poolmanager = PoolManager(
            num_pools=connections,
            maxsize=maxsize,
            block=block,
            ssl_context=ctx
        )

def req():
    session = requests.Session()
    # 给HTTPS请求挂载优化适配器
    session.mount('https://', OptimizedSSLAdapter())
    r = session.get('https://www.google.com/')

# 原测试逻辑保持不变
for i in range(10):
    thread_amount = 50
    threads = []
    s = time.time()
    for i in range(thread_amount):
        threads.append(threading.Thread(target=req))
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()
    e = time.time()
    total_time = e - s
    print(f"total time: {total_time} s")
    time.sleep(1)

3. 切换到支持HTTP/2的请求库

如果目标网站支持HTTP/2,改用httpx库替代requests,HTTP/2的多路复用特性可显著提升多并发请求性能:

import httpx
import threading
import time

def req():
    # 启用HTTP/2协议
    with httpx.Client(http2=True) as client:
        r = client.get('https://www.google.com/')

# 原测试逻辑保持不变
for i in range(10):
    thread_amount = 50
    threads = []
    s = time.time()
    for i in range(thread_amount):
        threads.append(threading.Thread(target=req))
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()
    e = time.time()
    total_time = e - s
    print(f"total time: {total_time} s")
    time.sleep(1)
  • 说明:安装httpx可执行pip install httpx[http2],它原生支持HTTP/2,在高并发场景下比基于HTTP/1.1的requests更高效,且OpenSSL 3.x对HTTP/2的支持优化更到位。

4. 通过环境变量临时调整OpenSSL线程配置

运行脚本前设置以下环境变量,优化OpenSSL的线程池和CPU特性适配:

export OPENSSL_threads=100
export OPENSSL_ia32cap="~0x200000200000000"
  • 说明:OPENSSL_threads增大OpenSSL的线程池容量,适配高并发请求;OPENSSL_ia32cap禁用x86架构下可能导致性能下降的CPU指令集特性,仅对当前会话生效,不影响全局环境。

内容的提问来源于stack exchange,提问作者Phil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 20:30:56