gpt4 book ai didi

python - SSL 认证错误 > 主机名不匹配

转载 作者:可可西里 更新时间:2023-11-01 07:06:53 24 4
gpt4 key购买 nike

我正在尝试使用 SSL 证书和带有以下行的 python 模块 PyMySQL 连接到 Google Cloud MYSQL 服务器:

connection = pymysql.connect(host=os.environ['SQL_HOST_IP'], user=os.environ['SQL_USER'], password = os.environ['SQL_PASSWORD'],
db='main', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor,
ssl={'key': 'client-key.pem', 'cert': 'client-cert.pem', 'ca': 'server-ca.pem'})

不幸的是,我不断收到以下错误:

ssl.CertificateError: hostname 'SQL_IP_ADDRESS' doesn't match '$ALIAS_FROM_SELF_SIGNED_SSL_CERT'

我已经查找了这个问题,但找不到不涉及 monkeypatching ssl 代码以跳过 ssl 验证的修复方法。我明确列出了 SQL 主机的 IP 地址,但 ssl 验证在 ssl.match_hostname 期间停止,因为 ssl 证书是使用不同的主机名自签名的。

我确定我的 key 有效,因为我可以使用 Ruby (Windows/Linux) 和 linux mysql CLI 连接它们。这似乎是 ssl.match_hostname 的问题。它类似于 this questionthis one但双方都回避了这个问题。

有没有办法在 Python 中正确处理自签名 SSL 证书。

最佳答案

尽管您的回答问题的解决方案在此处作为合并请求被拒绝:https://github.com/PyMySQL/PyMySQL/pull/555

您可以选择禁用 check_hostname。这适用于版本“0.7.11”

ssl_options = {
'key': 'client-key.pem',
'cert': 'client-cert.pem',
'ca': 'server-ca.pem',
'check_hostname': False
}

connection = pymysql.connect(
host=os.environ['SQL_HOST_IP'],
user=os.environ['SQL_USER'],
password = os.environ['SQL_PASSWORD'],
db='main',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
ssl=ssl_options
)

关于python - SSL 认证错误 > 主机名不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42596107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com