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

Ubuntu 10.04虚拟机中pip安装Flask依赖时SSL错误求助

问题描述

在Ubuntu 10.04虚拟机上为Flask Web应用安装依赖包时,因系统版本过旧持续遭遇SSL相关错误:

  • 修改/usr/local/lib/python2.7/urllib2.py添加特定链接的HTTP转HTTPS逻辑(如下代码)后,仍提示HTTP Error 403: SSL is required:
    --- /usr/local/lib/python2.7/urllib2.py.original
    +++ /usr/local/lib/python2.7/urllib2.py
    @@ -427,6 +427,9 @@
                 req = meth(req)
    
             response = self._open(req, data)
    +        if protocol == "http" and response.code == 403 :
    +            if isinstance(fullurl, basestring) and fullurl.startswith("http://pypi.python.org/packages/source/d/distribute/") :
    +                return    self.open(fullurl.replace("http://", "https://"), data = data, timeout = timeout)
    
             # post-process response
             meth_name = protocol+"_response"
    
  • 尝试指定HTTPS源执行pip install xxxx -i https://pypi.python.org/simple/时,收到错误<urlopen error [Errno 8] _ssl.c:480: EOF occurred in violation of protocol>
解决方案

1. 修复系统源(Ubuntu 10.04官方源已归档)

Ubuntu 10.04的官方源已停止维护,需要替换为归档源:
编辑/etc/apt/sources.list,将所有源地址替换为http://old-releases.ubuntu.com/ubuntu/,执行命令:

sed -i 's/archive.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
sed -i 's/security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
apt-get update

2. 升级OpenSSL以支持TLS 1.2

Ubuntu 10.04默认的OpenSSL版本不支持现代TLS协议,先尝试通过归档源升级:

apt-get install openssl libssl-dev

若归档源版本仍不满足,手动编译OpenSSL 1.0.2系列(最后支持Python2.7的版本):

wget https://www.openssl.org/source/old/1.0.2/openssl-1.0.2u.tar.gz
tar zxf openssl-1.0.2u.tar.gz
cd openssl-1.0.2u
./config --prefix=/usr/local/openssl shared zlib
make && make install
# 替换系统默认的OpenSSL
mv /usr/bin/openssl /usr/bin/openssl.old
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
ldconfig

3. 升级pip到Python2.7兼容的最后版本

旧版本pip不支持新TLS协议,升级到pip 20.3.4(Python2.7的最后兼容版本):

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
LD_LIBRARY_PATH=/usr/local/openssl/lib python get-pip.py

4. 安装Flask依赖

升级完成后,重新尝试安装依赖:

pip install flask

若仍有问题,使用支持旧协议的PyPI镜像:

pip install flask -i http://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com

5. 备选方案:离线安装

如果上述方法均失败,在其他机器下载依赖包的.whl或.tar.gz文件,传输到Ubuntu10.04虚拟机后离线安装:

pip install flask-2.0.3.tar.gz

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 23:15:35