gpt4 book ai didi

Python SSL 错误 CERTIFICATE_VERIFY_FAILED

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:57 24 4
gpt4 key购买 nike

我对我学校网站的请求有疑问。我在网上搜索了解决方案,但没有一个对我有用。我已经通过 pip 安装了 certifi,它不起作用。我已经重新安装了 openssl,但它不起作用。

>>> import ssl
>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2l 25 May 2017'

具体来说,SSL 验证是这里的问题。我可以用我的浏览器正确打开网页,没有任何 SSL 警告,但我不能用 Python。

那么,接下来我该做什么呢?希望有人能给我一点建议。非常感谢

最佳答案

通过阅读Python的requests docs ,

我发现了以下内容,其中指出:

When you are using the prepared request flow, keep in mind that it does not take into account the environment. This can cause problems if you are using environment variables to change the behaviour of requests. For example: Self-signed SSL certificates specified in REQUESTS_CA_BUNDLE will not be taken into account. As a result an SSL: CERTIFICATE_VERIFY_FAILED is thrown. You can get around this behaviour by explicity merging the environment settings into your session:

from requests import Request, Session

s = Session()
req = Request('GET', url)

prepped = s.prepare_request(req)

# Merge environment settings into session
settings = s.merge_environment_settings(prepped.url, None, None, None, None)
resp = s.send(prepped, **settings)

print(resp.status_code)

我建议阅读文档,因为我只是引用了原始文档的一部分。

除此之外,

Requests verifies SSL certificates for HTTPS requests, just like a web browser. By default, SSL verification is enabled, and Requests will throw a SSLError if it's unable to verify the certificate.

您可以使用verify 参数来提供指向CA_BUNDLE 文件或目录的路径,其中包含可信CA 的证书:

>>> requests.get('https://your-school-website-url.com', verify='/path/to/cacert_file')

Requests 也可以忽略验证SSL 证书 如果您设置verifyFalse:

>>> requests.get('https://kennethreitz.org', verify=False)
<Response [200]>

默认情况下verify 设置为True。选项验证仅适用于主机证书。

再次,请阅读 docs .

希望对您有所帮助!

关于Python SSL 错误 CERTIFICATE_VERIFY_FAILED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45989672/

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