gpt4 book ai didi

python-2.7 - 正在尝试加载 SSL 证书 : descriptor 'load_cert_chain' requires a '_ssl._SSLContext' object but received a 'str'

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

我正在尝试加载证书,以便在下载某些文件时绕过我们的公司防火墙。我使用 urllib2 下载这些文件,并尝试添加证书作为上下文。这曾经工作了一段时间,但现在不再工作了。我收到以下错误:

TypeError: descriptor 'load_cert_chain' requires a '_ssl._SSLContext' object but received a 'str'

Python 网站上的文档说明如下:

SSLContext.load_cert_chain(certfile, keyfile=None, password=None)

Load a private key and the corresponding certificate. The certfile string must be the path to a single file in PEM format containing the certificate as well as any number of CA certificates needed to establish the certificate’s authenticity. The keyfile string, if present, must point to a file containing the private key in. Otherwise the private key will be taken from certfile as well. See the discussion of Certificates for more information on how the certificate is stored in the certfile.

我的代码如下:

    cert = SSLContext.load_cert_chain('<abs_path_to_PEM_file>')
scheme, netloc, path, params, query, frag = urlparse(url)
auth, host = urllib2.splituser(netloc)
if auth:
url = urlunparse((scheme, host, path, params, query, frag))
req = urllib2.Request(url)
base64string = base64.encodestring(auth)[:-1]
basic = "Basic " + base64string
req.add_header("Authorization", basic)
else:
req = urllib2.Request(url)
url_obj = urllib2.urlopen(req, context=cert)
with open(tmp_path, 'wb') as fp:
fp.write(url_obj.read())
return tmp_path, url_obj.info()

它在那里说 certfile 参数应该是 PEM 文件所在路径的字符串。这正是我正在做的,但我收到了这个错误。我很困惑。我在这里做错了什么?

提前致谢!

最佳答案

.load_cert_chain 不是静态方法。您需要实例化一个 SSLContext 对象。在最简单的情况下,这可以通过方法完成

ctx = ssl.create_default_context()

还有那个

ctx.load_default_certs()

然后您可以按如下方式使用它:

url_obj = urllib2.urlopen(req, context=ctx)

关于python-2.7 - 正在尝试加载 SSL 证书 : descriptor 'load_cert_chain' requires a '_ssl._SSLContext' object but received a 'str' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308525/

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