Search code examples
pythonscrapytwisted

Scrapy contextfactory.py. NameError in iPython. Trying to used twisted library


I am trying to run a fetch command in scrapy shell. The error points to contextfactory.py. The twisted library is already included in my virtual environment. How do I resolve this error?

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



myClientCert = twisted.internet.ssl.PrivateCertificate.load(keyAndCert.read())

enter image description here

enter image description here


Solution

  • from twisted.internet.ssl import ClientContextFactory
    from twisted.internet.ssl import PrivateCertificate 
    
    myClientCert = twisted.internet.ssl.PrivateCertificate.load(keyAndCert.read())
    

    Line 1 puts the ClientContextFactory name into your scope. Line 2 puts the PrivateCertificate name into your scope. Line 4 tries to read two names from your scope: twisted and keyAndCert. Neither of these match the two names you have put in your scope (ClientContextFacotry and PrivateCertificate).

    Fortunately, the reason you are trying to use the twisted name, apparently, is to reach PrivateCertificate. You can replace twisted.internet.ssl.PrivateCertificate with the name that is actually in your scope, PrivateCertificate.

    Once you fix this, you'll get an error about keyAndCert since that is also not defined in your scope (unless it really is but you've omitted the code that defines it).