VS Code部署Flask应用至Azure AppService遇SSL错误求助
我是Python新手,尝试通过VS Code将Flask应用部署到Azure App Service。已创建AppService并通过虚拟环境生成requirements.txt文件,但部署时出现如下错误:
Warning: Retrying(Retry(total=4,connect=None, read=None, status=None, redirect=None)) after connection broken by 'SSLError(SSLEOFERROR(8,'EOF occurred in violation of protocol(_ssl.c:1000)'))' Retrying(Retry(total=3,connect=None, read=None, status=None, redirect=None)) after connection broken by 'SSLError(SSLEOFERROR(8,'EOF occurred in violation of protocol(_ssl.c:1000)'))' Retrying(Retry(total=2,connect=None, read=None, status=None, redirect=None)) after connection broken by 'SSLError(SSLEOFERROR(8,'EOF occurred in violation of protocol(_ssl.c:1000)'))' Retrying(Retry(total=1,connect=None, read=None, status=None, redirect=None)) after connection broken by 'SSLError(SSLEOFERROR(8,'EOF occurred in violation of protocol(_ssl.c:1000)'))' Retrying(Retry(total=0,connect=None, read=None, status=None, redirect=None)) after connection broken by 'SSLError(SSLEOFERROR(8,'EOF occurred in violation of protocol(_ssl.c:1000)'))' Error: Could not install packages due to an OSError:HTTPSConnectionPool(host='files.pythonhosted.org',port=443): Max retries with exceeded url....
试过Pipelines部署也失败,希望能通过VS Code成功完成部署。
解决方法
1. 检查App Service网络出站规则
确认App Service所在区域的网络未限制对files.pythonhosted.org的HTTPS访问。如果使用了虚拟网络或防火墙规则,需在Azure门户的App Service「网络」选项中,确保出站流量允许连接该域名的443端口。
2. 升级pip版本
本地虚拟环境先升级pip到最新版:
python -m pip install --upgrade pip
同时在requirements.txt开头添加pip>=23.0,保证Azure环境使用的pip支持最新SSL协议。
3. 切换到Azure内置PyPI镜像
在项目根目录创建pip.conf(Linux/macOS)或pip.ini(Windows)文件,内容如下:
[global] index-url = https://azurepipelines.pypi.azure.com/simple/
将该文件纳入部署包,Azure会自动使用这个国内访问更稳定的镜像源安装依赖。
4. 本地预构建依赖包再部署
本地先安装所有依赖到指定目录,生成包含完整依赖的部署包:
pip install -r requirements.txt --target ./site-packages
(替换为你的Python版本对应路径)
然后将整个项目目录(包括site-packages)打包成zip,通过VS Code的Azure扩展直接上传到App Service,跳过在线安装依赖的步骤。
5. 对齐本地与Azure的Python版本
确认本地使用的Python版本和Azure App Service配置的版本完全一致(比如都是3.9或3.10)。版本不匹配可能引发SSL相关依赖问题,可在Azure门户的App Service「配置」->「常规设置」中调整Python版本。
内容的提问来源于stack exchange,提问作者user25263310

