loadPEM方法中的Scrapy ResponseNeverReceived错误

2024-06-16 10:36:13 发布

您现在位置:Python中文网/ 问答频道 /正文

from OpenSSL import SSL

from twisted.internet.ssl import ClientContextFactory
from twisted.internet.ssl import PrivateCertificate
#from twisted.internet.ssl import DefaultOpenSSLContextFactory
from twisted.internet import ssl

class BrowserLikeContextFactory(ScrapyClientContextFactory):

            def creatorForNetloc(self, hostname, port):

            # trustRoot set to platformTrust() will use the platform's root CAs.
            #
            # This means that a website like https://www.cacert.org will be rejected
            # by default, since CAcert.org CA certificate is seldom shipped.

            with open ('path\\sample.pem') as f:

                keyAndCert = f.read()
                myClientCert = PrivateCertificate.loadPEM(keyAndCert)

              return optionsForClientTLS(hostname.decode("ascii"),

                trustRoot=platformTrust(),
                clientCertificate=myClientCert,
                extraCertificateOptions={
                'method': self._ssl_method,
              })

从上面的代码中,我试图加载一个组织的PEM文件(我有权使用该文件)来访问他们的站点。该文件存在于指定路径中,且具有匹配的文件名

enter image description here

但是,遇到了一个错误。这是文件的问题还是我如何实现代码的问题

enter image description here

我使用以下代码从.pfx文件创建了.pem文件

openssl pkcs12 -in sample.pfx -out sample.pem

Tags: 文件sample代码fromimportselfssltwisted