Cnly Cnly - 3 months ago 18
Python Question

Scrapy / OpenSSL scraping HTTPS sites: AttributeError: 'module' object has no attribute 'SSL_CTX_set_session_id_context'

I was trying to crawl an HTTPS site but got the following error:

2016-05-14 20:11:21 [scrapy] ERROR: Error downloading <GET https://www.anHTTPSsite.com/>
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/twisted/internet/defer.py", line 1126, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/usr/local/lib/python3.4/dist-packages/twisted/python/failure.py", line 389, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/middleware.py", line 43, in process_request
defer.returnValue((yield download_func(request=request,spider=spider)))
File "/usr/local/lib/python3.4/dist-packages/scrapy/utils/defer.py", line 45, in mustbe_deferred
result = f(*args, **kw)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/handlers/__init__.py", line 65, in download_request
return handler.download_request(request, spider)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/handlers/http11.py", line 60, in download_request
return agent.download_request(request)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/handlers/http11.py", line 281, in download_request
method, to_bytes(url, encoding='ascii'), headers, bodyproducer)
File "/usr/local/lib/python3.4/dist-packages/twisted/web/client.py", line 1594, in request
endpoint = self._getEndpoint(parsedURI)
File "/usr/local/lib/python3.4/dist-packages/twisted/web/client.py", line 1578, in _getEndpoint
return self._endpointFactory.endpointForURI(uri)
File "/usr/local/lib/python3.4/dist-packages/twisted/web/client.py", line 1454, in endpointForURI
uri.port)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/contextfactory.py", line 57, in creatorForNetloc
return ScrapyClientTLSOptions(hostname.decode("ascii"), self.getContext())
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/contextfactory.py", line 54, in getContext
return self.getCertificateOptions().getContext()
File "/usr/local/lib/python3.4/dist-packages/twisted/internet/_sslverify.py", line 1618, in getContext
self._context = self._makeContext()
File "/usr/local/lib/python3.4/dist-packages/twisted/internet/_sslverify.py", line 1656, in _makeContext
ctx.set_session_id(sessionName)
File "/usr/local/lib/python3.4/dist-packages/OpenSSL/SSL.py", line 719, in set_session_id
_lib.SSL_CTX_set_session_id_context(
AttributeError: 'module' object has no attribute 'SSL_CTX_set_session_id_context'


However, when I try to crawl its HTTP version(simply changing the protocol in the URL from
https
to
http
), the error is gone.

EDIT:
scrapy version -v
output:

Scrapy : 1.1.0
lxml : 3.4.4.0
libxml2 : 2.9.2
Twisted : 16.1.1
Python : 3.4.3+ (default, Oct 14 2015, 16:03:50) - [GCC 5.2.1 20151010]
pyOpenSSL : 16.0.0 (OpenSSL 1.0.2d 9 Jul 2015)
Platform : Linux-4.2.0-36-generic-x86_64-with-Ubuntu-15.10-wily


Any help will be apreciated.

Answer

I had the same issue. It appears to be related to recent changes in pyOpenSSL. Downgrading to 0.15.1 solved it for me. Before doing so, I recommend looking at the changelog of pyOpenSSL to see if you feel that downgrading is worth it.

On OSX, this should work:

sudo pip install pyOpenSSL==0.15.1