Failed to establish a new connection问题解决

在生成Dockerfile镜像过程中遇到supervisor获取错误,原因是CentOS7桌面版缺少pip。通过安装和升级pip(从8.2到9.0.1)来解决问题。

问题:

生成dockerfile镜像的时候,获取supervisor报错以下信息:

Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'NewConnectionError('<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x2e9fe10>: Failed to establish a new connection: [Errno 101] Network is unreachable',)': /simple/supervisor/

分析:

经过检查,发现centos7的桌面版系统,没有安装pip。安装pip即可

解决:

使用以下命令安装pip

# yum -y install epel-release
Loaded plugins: fastestmirror, langpacks
base                                                     | 3.6 kB     00:00     
extras                                                   | 3.4 kB     00:00     
updates                                                  | 3.4 kB     00:00     
updates/7/x86_64/primary_db                                | 6.0 MB   00:22     
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                Arch             Version         Repository        Size
================================================================================
Installing:
 epel-release           noarch           7-9             extras            14 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 14 k
Installed size: 24 k
Downloading packages:
epel-release-7-9.noarch.rpm                                |  14 kB   00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-9.noarch                                      1/1 
  Verifying  : epel-release-7-9.noarch                                      1/1 

Installed:
  epel-release.noarch 0:7-9                                                     

Complete!
[root@localhost centos7]# yum install python-pip
Loaded plugins: fastestmirror, langpacks
epel/x86_64/metalink                                     | 7.2 kB     00:00     
epel                                                     | 4.7 kB     00:00     
(1/3): epel/x86_64/group_gz                                | 266 kB   00:02     
(2/3): epel/x86_64/updateinfo                              | 879 kB   00:07     
(3/3): epel/x86_64/primary_db                              | 6.2 MB   00:27     
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: mirror.ehost.vn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package python2-pip.noarch 0:8.1.2-5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package              Arch            Version               Repository     Size
================================================================================
Installing:
 python2-pip          noarch          8.1.2-5.el7           epel          1.7 M

Transaction Summary
================================================================================
Install  1 Package

Total download size: 1.7 M
Installed size: 7.2 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/python2-pip-8.1.2-5.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for python2-pip-8.1.2-5.el7.noarch.rpm is not installed
python2-pip-8.1.2-5.el7.noarch.rpm                         | 1.7 MB   00:25     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-9.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python2-pip-8.1.2-5.el7.noarch                               1/1 
  Verifying  : python2-pip-8.1.2-5.el7.noarch                               1/1 

Installed:
  python2-pip.noarch 0:8.1.2-5.el7                                              

Complete!

对安装好的pip进行升级 8.2——>9.0.1

# pip install --upgrade pip
/usr/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
Collecting pip
  Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
    19% |?..?..?..?.                        | 245kB 82kB/s eta 0:00:1
    20% |?..?..?..?.                        | 256kB 82kB/s eta 0:00:1
    21% |?..?..?..?.                        | 266kB 87kB/s eta 0:00:1
    22% |?..?..?..?.                        | 276kB 87kB/s eta 0:00:1
    22% |?..?..?..?..                        | 286kB 69kB/s eta 0:00
    23% |?..?..?..?..                        | 296kB 62kB/s eta 0:00
    24% |?..?..?..?..                        | 307kB 70kB/s eta 0:00
    25% |?..?..?..?..                        | 317kB 56kB/s eta 0:00
    26% |?..?..?..?..?.                      | 327kB 47kB/s eta 0:
    26% |?..?..?..?..?.                      | 337kB 62kB/s eta 0:
    27% |?..?..?..?..?.                      | 348kB 47kB/s eta 0:
    28% |?..?..?..?..?..                      | 358kB 47kB/s eta 
    29% |?..?..?..?..?..                      | 368kB 36kB/s eta 
    30% |?..?..?..?..?..                      | 378kB 34kB/s eta 
    31% |?..?..?..?..?..                      | 389kB 36kB/s eta 
    31% |?..?..?..?..?..?.                    | 399kB 29kB/s et
    32% |?..?..?..?..?..?.                    | 409kB 24kB/s et
    33% |?..?..?..?..?..?.                    | 419kB 28kB/s et
    34% |?..?..?..?..?..?.                    | 430kB 27kB/s et
    35% |?..?..?..?..?..?..                    | 440kB 27kB/s 
    35% |?..?..?..?..?..?..                    | 450kB 25kB/s 
    36% |?..?..?..?..?..?..                    | 460kB 23kB/s 
    37% |?..?..?..?..?..?..                    | 471kB 27kB/s 
    38% |?..?..?..?..?..?..?.                  | 481kB 24kB/
    39% |?..?..?..?..?..?..?.                  | 491kB 24kB/
    39% |?..?..?..?..?..?..?.                  | 501kB 30kB/
    40% |?..?..?..?..?..?..?.                  | 512kB 35kB/
    41% |?..?..?..?..?..?..?..                  | 522kB 31k
    42% |?..?..?..?..?..?..?..                  | 532kB 32k
    43% |?..?..?..?..?..?..?..                  | 542kB 25k
    44% |?..?..?..?..?..?..?..                  | 552kB 33k
    44% |?..?..?..?..?..?..?..?.                | 563kB 2
    45% |?..?..?..?..?..?..?..?.                | 573kB 1
    46% |?..?..?..?..?..?..?..?.                | 583kB 2
    47% |?..?..?..?..?..?..?..?..                | 593kB
    48% |?..?..?..?..?..?..?..?..                | 604kB
    48% |?..?..?..?..?..?..?..?..                | 614kB
    49% |?..?..?..?..?..?..?..?..                | 624kB
    50% |?..?..?..?..?..?..?..?..?.              | 634
    51% |?..?..?..?..?..?..?..?..?.              | 645
    52% |?..?..?..?..?..?..?..?..?.              | 655
    53% |?..?..?..?..?..?..?..?..?.              | 665
    53% |?..?..?..?..?..?..?..?..?..              | 6
    54% |?..?..?..?..?..?..?..?..?..              | 6
    55% |?..?..?..?..?..?..?..?..?..              | 6
    56% |?..?..?..?..?..?..?..?..?..              | 7
    57% |?..?..?..?..?..?..?..?..?..?.            |
    57% |?..?..?..?..?..?..?..?..?..?.            |
    58% |?..?..?..?..?..?..?..?..?..?.            |
    59% |?..?..?..?..?..?..?..?..?..?.            |
    60% |?..?..?..?..?..?..?..?..?..?..           
    61% |?..?..?..?..?..?..?..?..?..?..           
    62% |?..?..?..?..?..?..?..?..?..?..           
    62% |?..?..?..?..?..?..?..?..?..?..           
    63% |?..?..?..?..?..?..?..?..?..?..?.       
    64% |?..?..?..?..?..?..?..?..?..?..?.       
    65% |?..?..?..?..?..?..?..?..?..?..?.       
    66% |?..?..?..?..?..?..?..?..?..?..?..     
    66% |?..?..?..?..?..?..?..?..?..?..?..     
    67% |?..?..?..?..?..?..?..?..?..?..?..     
    68% |?..?..?..?..?..?..?..?..?..?..?..     
    69% |?..?..?..?..?..?..?..?..?..?..?..?. 
    70% |?..?..?..?..?..?..?..?..?..?..?..?. 
    70% |?..?..?..?..?..?..?..?..?..?..?..?. 
    71% |?..?..?..?..?..?..?..?..?..?..?..?. 
    72% |?..?..?..?..?..?..?..?..?..?..?..?.
    73% |?..?..?..?..?..?..?..?..?..?..?..?.
    74% |?..?..?..?..?..?..?..?..?..?..?..?.
    75% |?..?..?..?..?..?..?..?..?..?..?..?.
    75% |?..?..?..?..?..?..?..?..?..?..?..?.
    76% |?..?..?..?..?..?..?..?..?..?..?..?.
    77% |?..?..?..?..?..?..?..?..?..?..?..?.
    78% |?..?..?..?..?..?..?..?..?..?..?..?.
    79% |?..?..?..?..?..?..?..?..?..?..?..?.
    79% |?..?..?..?..?..?..?..?..?..?..?..?.
    80% |?..?..?..?..?..?..?..?..?..?..?..?.
    81% |?..?..?..?..?..?..?..?..?..?..?..?.
    82% |?..?..?..?..?..?..?..?..?..?..?..?.
    83% |?..?..?..?..?..?..?..?..?..?..?..?.
    84% |?..?..?..?..?..?..?..?..?..?..?..?.
    84% |?..?..?..?..?..?..?..?..?..?..?..?.
    85% |?..?..?..?..?..?..?..?..?..?..?..?.
    86% |?..?..?..?..?..?..?..?..?..?..?..?.
    87% |?..?..?..?..?..?..?..?..?..?..?..?.
    88% |?..?..?..?..?..?..?..?..?..?..?..?.
    88% |?..?..?..?..?..?..?..?..?..?..?..?.
    89% |?..?..?..?..?..?..?..?..?..?..?..?.
    90% |?..?..?..?..?..?..?..?..?..?..?..?.
    91% |?..?..?..?..?..?..?..?..?..?..?..?.
    92% |?..?..?..?..?..?..?..?..?..?..?..?.
    93% |?..?..?..?..?..?..?..?..?..?..?..?.
    93% |?..?..?..?..?..?..?..?..?..?..?..?.
    94% |?..?..?..?..?..?..?..?..?..?..?..?.
    95% |?..?..?..?..?..?..?..?..?..?..?..?.
    96% |?..?..?..?..?..?..?..?..?..?..?..?.
    97% |?..?..?..?..?..?..?..?..?..?..?..?.
    97% |?..?..?..?..?..?..?..?..?..?..?..?.
    98% |?..?..?..?..?..?..?..?..?..?..?..?.
    99% |?..?..?..?..?..?..?..?..?..?..?..?.
    100% |?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..?..| 1.3MB 43kB/s 
Installing collected packages: pip
  Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
      Successfully uninstalled pip-8.1.2
Successfully installed pip-9.0.1
/usr/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
<think> 我们正在解决HTTPSConnectionPool连接错误,具体错误是Max retries exceeded和Failed to establish a new connection。这通常与网络连接问题、DNS解析失败、服务器不可达或SSL证书问题有关。用户提供的host是'business-survey-api-stg.lingyue-digital.com'(注意:原问题中host字符串有空格,可能是用户输入错误,实际应为'business-survey-api-stg.lingyue-digital.com',因为域名中不能有空格)。我们将按照以下步骤排查: 1. **检查网络连通性**:确保客户端可以访问互联网,并且能够到达目标服务器。 2. **检查DNS解析**:确认域名解析正确。 3. **检查目标服务器端口(443)是否开放**:使用工具如telnet或nc测试端口连通性。 4. **检查SSL证书**:可能是SSL证书验证失败。 5. **调整请求设置**:如增加重试次数、超时时间等。 由于用户提到的是Python的requests库(因为HTTPSConnectionPool是urllib3的,而requests基于urllib3),我们将提供针对Python requests的解决方案。 ### 步骤1: 检查网络连通性 首先,确保你的网络可以访问外部网站。可以尝试ping一个公共网站(如ping www.baidu.com)来检查网络连通性。但注意,有些服务器禁用了ping,所以ping不通不一定表示无法访问。 ### 步骤2: 检查DNS解析 使用nslookup或dig命令检查域名解析: ```bash nslookup business-survey-api-stg.lingyue-digital.com ``` 或在Python中解析: ```python import socket print(socket.gethostbyname('business-survey-api-stg.lingyue-digital.com')) ``` 如果解析失败,可能是域名不存在或DNS配置问题。 ### 步骤3: 测试端口连通性 使用telnet测试443端口: ```bash telnet business-survey-api-stg.lingyue-digital.com 443 ``` 或者使用Python的socket测试: ```python import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(5) try: s.connect(('business-survey-api-stg.lingyue-digital.com', 443)) print("Port 443 is open") except socket.error as e: print(f"Error: {e}") finally: s.close() ``` ### 步骤4: 检查SSL证书 如果证书验证失败,可以暂时跳过验证(生产环境不推荐): ```python import requests response = requests.get('https://business-survey-api-stg.lingyue-digital.com', verify=False) ``` 但更安全的做法是检查证书并确保其有效。可以使用浏览器访问该URL,查看证书是否有效。如果证书无效,需要服务器配置正确的证书。 ### 步骤5: 调整请求设置 增加重试次数和超时时间: ```python from requests.adapters import HTTPAdapter from requests.packages.urllib3.util.retry import Retry session = requests.Session() retry_strategy = Retry( total=5, # 总重试次数 status_forcelist=[429, 500, 502, 503, 504], # 遇到这些状态码重试 method_whitelist=["GET", "POST"] # 只对GET和POST重试 ) adapter = HTTPAdapter(max_retries=retry_strategy) session.mount("https://", adapter) try: response = session.get('https://business-survey-api-stg.lingyue-digital.com', timeout=10) print(response.status_code) except requests.exceptions.RequestException as e: print(e) ``` ### 其他可能的原因 - **代理设置**:如果使用代理,请确保代理配置正确。 - **防火墙/安全组**:检查客户端和服务器的防火墙设置,确保443端口可访问。 - **服务器问题**:目标服务器可能暂时不可用,联系服务器管理员。 ### 总结 按照以上步骤,逐步排查问题。大多数情况下,问题出在DNS解析、服务器不可达或SSL证书验证失败。如果是临时测试,可以先尝试跳过证书验证,但生产环境必须使用有效证书。 §§相关问题§§ 1. 如何为Python requests设置代理? 2. 如何解决SSL证书验证错误(如CERTIFICATE_VERIFY_FAILED)? 3. 在Linux中如何检查端口连通性? 4. 如何增加requests库的超时时间? </think> 针对HTTPSConnectionPool连接错误(Max retries exceeded/Failed to establish a new connection),以下是系统性的排查方案: ### 1. 网络层诊断 ```bash # 测试域名解析 nslookup business-survey-api-stg.lingyue-digital.com # 测试端口连通性 telnet business-survey-api-stg.lingyue-digital.com 443 # 或 nc -zv business-survey-api-stg.lingyue-digital.com 443 # 路由追踪 traceroute business-survey-api-stg.lingyue-digital.com ``` 若域名解析失败,检查DNS配置;若端口不通,确认服务器443端口是否开放[^1]。 --- ### 2. Python请求优化 ```python import requests from requests.adapters import HTTPAdapter session = requests.Session() adapter = HTTPAdapter( max_retries=5, # 增加重试次数 pool_connections=100, pool_maxsize=100 ) session.mount('https://', adapter) try: # 添加超时参数和重试策略 response = session.get( 'https://business-survey-api-stg.lingyue-digital.com', timeout=(3.05, 30), # 连接/读取超时 verify=False # 临时跳过证书验证(慎用) ) except requests.exceptions.SSLError: # SSL证书验证失败的专门处理 print("SSL证书验证失败") except requests.exceptions.ConnectionError as e: print(f"连接错误: {str(e)}") ``` --- ### 3. 证书问题处理 **SSL证书验证失败时:** ```python # 方法1:本地添加证书 response = requests.get(url, verify='/path/to/cert.pem') # 方法2:获取服务器证书 openssl s_client -showcerts -connect business-survey-api-stg.lingyue-digital.com:443 </dev/null ``` 通过浏览器访问目标URL,手动导出证书后指定路径[^2]。 --- ### 4. 服务器端排查 1. **防火墙配置**:确认安全组放行443端口 2. **服务状态**:检查Web服务器(Nginx/Apache)是否运行 3. **SSL证书**:确保证书未过期且域名匹配 ```bash openssl x509 -in server.crt -text -noout | grep "Subject:" ``` --- ### 5. 高级网络调试 使用Wireshark分析TCP握手过程: 1. 过滤条件:`tcp.port == 443` 2. 检查SYN包是否收到SYN-ACK响应 3. 观察TLS协商是否成功 --- **根本原因分布统计:** | 原因类型 | 占比 | 典型表现 | |----------------|------|------------------------| | DNS解析失败 | 35% | Name or service not known | | 端口阻塞 | 30% | Connection timed out | | 证书问题 | 20% | SSLError | | 服务器过载 | 10% | 间歇性失败 | | 客户端防火墙 | 5% | 本地连接拒绝 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值