Scrapy, a fast high-level web crawling & scraping framework for Python.
fixed tls in Twisted 17+
Mikhail Korobov1bc4d8b6b6cf
diff --git a/scrapy/core/downloader/tls.py b/scrapy/core/downloader/tls.pyindex 955b7630c..498e3d60f 100644--- a/scrapy/core/downloader/tls.py+++ b/scrapy/core/downloader/tls.py@@ -1,6 +1,8 @@import loggingfrom OpenSSL import SSL+from scrapy import twisted_version+logger = logging.getLogger(__name__)@@ -18,11 +20,17 @@ openssl_methods = {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_STARTexcept ImportError:SSL_CB_HANDSHAKE_START = 0x10@@ -30,10 +38,17 @@ try:from twisted.internet.ssl import AcceptableCiphersfrom 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 @@ try: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 @@ try: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