Cnly Cnly - 1 year ago 143
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>
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/twisted/internet/", line 1126, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/usr/local/lib/python3.4/dist-packages/twisted/python/", line 389, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/", line 43, in process_request
defer.returnValue((yield download_func(request=request,spider=spider)))
File "/usr/local/lib/python3.4/dist-packages/scrapy/utils/", line 45, in mustbe_deferred
result = f(*args, **kw)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/handlers/", line 65, in download_request
return handler.download_request(request, spider)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/handlers/", line 60, in download_request
return agent.download_request(request)
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/handlers/", line 281, in download_request
method, to_bytes(url, encoding='ascii'), headers, bodyproducer)
File "/usr/local/lib/python3.4/dist-packages/twisted/web/", line 1594, in request
endpoint = self._getEndpoint(parsedURI)
File "/usr/local/lib/python3.4/dist-packages/twisted/web/", line 1578, in _getEndpoint
return self._endpointFactory.endpointForURI(uri)
File "/usr/local/lib/python3.4/dist-packages/twisted/web/", line 1454, in endpointForURI
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/", line 57, in creatorForNetloc
return ScrapyClientTLSOptions(hostname.decode("ascii"), self.getContext())
File "/usr/local/lib/python3.4/dist-packages/scrapy/core/downloader/", line 54, in getContext
return self.getCertificateOptions().getContext()
File "/usr/local/lib/python3.4/dist-packages/twisted/internet/", line 1618, in getContext
self._context = self._makeContext()
File "/usr/local/lib/python3.4/dist-packages/twisted/internet/", line 1656, in _makeContext
File "/usr/local/lib/python3.4/dist-packages/OpenSSL/", line 719, in set_session_id
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
), the error is gone.

scrapy version -v

Scrapy : 1.1.0
lxml :
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 Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download