Search code examples
pythonscrapyopensslssl-certificatetwisted

Python Twisted Scrapy AttributeError: 'DefaultOpenSSLContextFactory' object has no attribute 'privateKey'


In this code, I am trying to use a certificate .key and .pem file for a private page which I am authorized to use.

from OpenSSL import SSL
from twisted.internet import ssl

class BrowserLikeContextFactory(ScrapyClientContextFactory):


        def creatorForNetloc(self, hostname, port):


            certificate = ssl.DefaultOpenSSLContextFactory('path\\sample.key','path\\sample.pem')

            return optionsForClientTLS(hostname.decode("ascii"),
                trustRoot=platformTrust(),
                clientCertificate=certificate,
                extraCertificateOptions={
                'method': self._ssl_method,
                    })

The two files exist in the path specified. I tried a fetch command in Scrapy and this was the first result.

enter image description here

However, after putting in the PEM pass phrase, this error was encountered? How do I resolve this?

enter image description here


Solution

  • certificate = ssl.DefaultOpenSSLContextFactory('path\sample.key','path\sample.pem')

    This isn't a certificate. Later, something tries to use it as if it were and gets an attribute error because it doesn't have the privateKey attribute like a certificate would/should.

    Try something like

    with open("path/sample.key") as k:
        with open("path/sample.pem") as p:
            certificate = ssl.PrivateCertificate.loadPEM(k.read() + p.read())
    

    instead.