diff options
author | Sho Minagawa <minagawa-sho@users.noreply.github.com> | 2017-02-04 02:44:29 +0900 |
---|---|---|
committer | Dana Powers <dana.powers@gmail.com> | 2017-02-03 09:44:29 -0800 |
commit | 641b6399d89687721e7a88524d6ed288a43ce8ad (patch) | |
tree | e0e8cbaf77ca5f086e02464a1da34ea052bde2e7 | |
parent | bfd1138399962209be5b709ad74f33bd12d2f890 (diff) | |
download | kafka-python-641b6399d89687721e7a88524d6ed288a43ce8ad.tar.gz |
Add support for Python built without ssl (#939) (#954)
-rw-r--r-- | kafka/conn.py | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/kafka/conn.py b/kafka/conn.py index bb9df69..c7a077c 100644 --- a/kafka/conn.py +++ b/kafka/conn.py @@ -7,7 +7,6 @@ import logging import io from random import shuffle import socket -import ssl import time import traceback @@ -31,19 +30,28 @@ log = logging.getLogger(__name__) DEFAULT_KAFKA_PORT = 9092 -# support older ssl libraries try: - ssl.SSLWantReadError - ssl.SSLWantWriteError - ssl.SSLZeroReturnError -except: - log.warning('Old SSL module detected.' - ' SSL error handling may not operate cleanly.' - ' Consider upgrading to Python 3.3 or 2.7.9') - ssl.SSLWantReadError = ssl.SSLError - ssl.SSLWantWriteError = ssl.SSLError - ssl.SSLZeroReturnError = ssl.SSLError - + import ssl + ssl_available = True + try: + SSLWantReadError = ssl.SSLWantReadError + SSLWantWriteError = ssl.SSLWantWriteError + SSLZeroReturnError = ssl.SSLZeroReturnError + except: + # support older ssl libraries + log.warning('Old SSL module detected.' + ' SSL error handling may not operate cleanly.' + ' Consider upgrading to Python 3.3 or 2.7.9') + SSLWantReadError = ssl.SSLError + SSLWantWriteError = ssl.SSLError + SSLZeroReturnError = ssl.SSLError +except ImportError: + # support Python without ssl libraries + ssl_available = False + class SSLWantReadError(Exception): + pass + class SSLWantWriteError(Exception): + pass class ConnectionStates(object): DISCONNECTING = '<disconnecting>' @@ -177,6 +185,9 @@ class BrokerConnection(object): (socket.SOL_SOCKET, socket.SO_SNDBUF, self.config['send_buffer_bytes'])) + if self.config['security_protocol'] in ('SSL', 'SASL_SSL'): + assert ssl_available, "Python wasn't built with SSL support" + if self.config['security_protocol'] in ('SASL_PLAINTEXT', 'SASL_SSL'): assert self.config['sasl_mechanism'] in self.SASL_MECHANISMS, ( 'sasl_mechanism must be in ' + ', '.join(self.SASL_MECHANISMS)) @@ -368,9 +379,9 @@ class BrokerConnection(object): self._sock.do_handshake() return True # old ssl in python2.6 will swallow all SSLErrors here... - except (ssl.SSLWantReadError, ssl.SSLWantWriteError): + except (SSLWantReadError, SSLWantWriteError): pass - except ssl.SSLZeroReturnError: + except SSLZeroReturnError: log.warning('SSL connection closed by server during handshake.') self.close(Errors.ConnectionError('SSL connection closed by server during handshake')) # Other SSLErrors will be raised to user @@ -608,7 +619,7 @@ class BrokerConnection(object): self.close(error=Errors.ConnectionError('socket disconnected')) return None self._rbuffer.write(data) - except ssl.SSLWantReadError: + except SSLWantReadError: return None except ConnectionError as e: if six.PY2 and e.errno == errno.EWOULDBLOCK: @@ -646,7 +657,7 @@ class BrokerConnection(object): self.close(error=Errors.ConnectionError('socket disconnected')) return None self._rbuffer.write(data) - except ssl.SSLWantReadError: + except SSLWantReadError: return None except ConnectionError as e: # Extremely small chance that we have exactly 4 bytes for a |