From de65ad3fb1e90f6fcffbcc74bfa6aff8ff65e14f Mon Sep 17 00:00:00 2001 From: Mikhail Korobov Date: Mon, 13 Feb 2017 18:44:39 +0500 Subject: [PATCH 1/2] TST replace Ubuntu 12.04 tox environment with 14.04 --- tox.ini | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tox.ini b/tox.ini index 0fdea11bbd3..bbf50b73374 100644 --- a/tox.ini +++ b/tox.ini @@ -21,16 +21,16 @@ passenv = commands = py.test --cov=scrapy --cov-report= {posargs:scrapy tests} -[testenv:precise] +[testenv:trusty] basepython = python2.7 deps = pyOpenSSL==0.13 - lxml==2.3.2 - Twisted==11.1.0 - boto==2.2.2 - Pillow<2.0 + lxml==3.3.3 + Twisted==13.2.0 + boto==2.20.1 + Pillow==2.3.0 cssselect==0.9.1 - zope.interface==3.6.1 + zope.interface==4.0.5 -rtests/requirements.txt [testenv:jessie] From 1bc4d8b6b6cf84c1785a6ad69abf37b4f0114bb3 Mon Sep 17 00:00:00 2001 From: Mikhail Korobov Date: Mon, 13 Feb 2017 20:03:53 +0500 Subject: [PATCH 2/2] fixed tls in Twisted 17+ --- scrapy/core/downloader/tls.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/scrapy/core/downloader/tls.py b/scrapy/core/downloader/tls.py index 955b7630c40..498e3d60fb9 100644 --- a/scrapy/core/downloader/tls.py +++ b/scrapy/core/downloader/tls.py @@ -1,6 +1,8 @@ import logging from OpenSSL import SSL +from scrapy import twisted_version + logger = logging.getLogger(__name__) @@ -18,11 +20,17 @@ METHOD_TLSv12: getattr(SSL, 'TLSv1_2_METHOD', 6), # TLS 1.2 only } -# ClientTLSOptions requires a recent-enough version of Twisted -try: +if twisted_version >= (14, 0, 0): + # ClientTLSOptions requires a recent-enough version of Twisted. + # Not having ScrapyClientTLSOptions should not matter for older + # Twisted versions because it is not used in the fallback + # ScrapyClientContextFactory. # taken from twisted/twisted/internet/_sslverify.py + try: + # XXX: this try-except is not needed in Twisted 17.0.0+ because + # it requires pyOpenSSL 0.16+. from OpenSSL.SSL import SSL_CB_HANDSHAKE_DONE, SSL_CB_HANDSHAKE_START except ImportError: SSL_CB_HANDSHAKE_START = 0x10 @@ -30,10 +38,17 @@ from twisted.internet.ssl import AcceptableCiphers from twisted.internet._sslverify import (ClientTLSOptions, - _maybeSetHostNameIndication, verifyHostname, VerificationError) + if twisted_version < (17, 0, 0): + from twisted.internet._sslverify import _maybeSetHostNameIndication + set_tlsext_host_name = _maybeSetHostNameIndication + else: + def set_tlsext_host_name(connection, hostNameBytes): + connection.set_tlsext_host_name(hostNameBytes) + + class ScrapyClientTLSOptions(ClientTLSOptions): """ SSL Client connection creator ignoring certificate verification errors @@ -46,7 +61,7 @@ class ScrapyClientTLSOptions(ClientTLSOptions): def _identityVerifyingInfoCallback(self, connection, where, ret): if where & SSL_CB_HANDSHAKE_START: - _maybeSetHostNameIndication(connection, self._hostnameBytes) + set_tlsext_host_name(connection, self._hostnameBytes) elif where & SSL_CB_HANDSHAKE_DONE: try: verifyHostname(connection, self._hostnameASCII) @@ -62,8 +77,3 @@ def _identityVerifyingInfoCallback(self, connection, where, ret): self._hostnameASCII, repr(e))) DEFAULT_CIPHERS = AcceptableCiphers.fromOpenSSLCipherString('DEFAULT') - -except ImportError: - # ImportError should not matter for older Twisted versions - # as the above is not used in the fallback ScrapyClientContextFactory - pass