botocore.exceptions.SSLError:SSL 验证在 WIndows 上失败



下面的代码用于获取区域。

import boto3
ec2 = boto3.client('ec2', 'region-name')
print(ec2.describe_regions())

在我的机器上执行此代码时,我收到此错误。

botocore.exceptions.SSL 环境:https://ec2.region-name.amazonaws.com/的 SSL 验证失败 [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1108)

我正在使用VS 代码作为我的编辑器在 Windows 10 机器上运行此代码。我在寻找他们需要安装文件的其他答案Install Certificates.command。但是,看起来它仅在macOS上找到。

有人可以告诉我这个问题的原因吗?

此外,上周收到AWS的通知,他们正在将所有AWS FIPS终端节点更新为TLS 1.2。 因此需要连接到 TLS 版本 1.2 FIPS 端点。我在这里检查了我的TLS版本。它说我有TLS版本1.2。有什么关系吗?因为在此通知之前,我的脚本运行良好。

请有人告诉此错误的原因和可能的更正。另外,如果我提到我的理解错误,请纠正我。

[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate是因为 Pythonssl库在本地计算机上找不到要验证的证书。

调试的一种方法是查看是否将ca_bundle设置为其他内容:

python -c "from botocore.session import Session; print(Session().get_config_variable('ca_bundle'))"

如果它不打印任何内容,则使用默认路径。您可以通过以下方式检查默认路径:

python -c "import ssl; print(ssl.get_default_verify_paths())"

如果ca_bundle打印某些内容,则由AWS_CA_BUNDLE环境变量或过去的aws configure set default.ca_bundle <some path>设置。如果您不小心将其设置在那里,还要检查~/.aws/config(Windows的配置文件位置:https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)。

Install Certificates.command基本上是一个 Python 脚本,您可以自己运行 https://gist.github.com/marschhuynh/31c9375fc34a3e20c2d3b9eb8131d8f3 .另存为install-cert.pypython install-cert.py运行

也许是一个边缘情况,但我在向 docker 容器发送请求时遇到了这个问题,对我来说的修复是在http://localhost:8000而不是https://localhost:8000时命中 docker 容器,因为容器无法接收 SSL 请求。希望这对这种特殊情况下的任何人都有帮助!

import boto3    
from urllib3.exceptions import InsecureRequestWarning    
from urllib3 import disable_warnings    
disable_warnings(InsecureRequestWarning)
session = boto3.Session(profile_name='dev')    
client = session.client('ec2', verify=False)

最新更新