From 5e1fae03b374dc6a32c525826398d21943a2c792 Mon Sep 17 00:00:00 2001 From: Andrey Kosyakov Date: Wed, 25 Mar 2015 05:46:49 -0700 Subject: [PATCH] Fix exception in recv() when ssl module is not available --- websocket/_core.py | 18 +----------------- websocket/_http.py | 17 +---------------- websocket/_socket.py | 1 + websocket/_ssl_compat.py | 41 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 websocket/_ssl_compat.py diff --git a/websocket/_core.py b/websocket/_core.py index 53a25a5..c24b118 100644 --- a/websocket/_core.py +++ b/websocket/_core.py @@ -25,23 +25,6 @@ from __future__ import print_function import six import socket -try: - import ssl - from ssl import SSLError - if hasattr(ssl, "match_hostname"): - from ssl import match_hostname - else: - from backports.ssl_match_hostname import match_hostname - - HAVE_SSL = True -except ImportError: - # dummy class of SSLError for ssl none-support environment. - class SSLError(Exception): - pass - - HAVE_SSL = False - - if six.PY3: from base64 import encodebytes as base64encode else: @@ -60,6 +43,7 @@ from ._url import * from ._logging import * from ._http import * from ._handshake import * +from ._ssl_compat import * """ websocket python client. diff --git a/websocket/_http.py b/websocket/_http.py index 31cfcb8..f612736 100644 --- a/websocket/_http.py +++ b/websocket/_http.py @@ -24,22 +24,6 @@ import six import socket import errno -try: - import ssl - from ssl import SSLError - if hasattr(ssl, "match_hostname"): - from ssl import match_hostname - else: - from backports.ssl_match_hostname import match_hostname - - HAVE_SSL = True -except ImportError: - # dummy class of SSLError for ssl none-support environment. - class SSLError(Exception): - pass - - HAVE_SSL = False - if six.PY3: from base64 import encodebytes as base64encode else: @@ -49,6 +33,7 @@ from ._logging import * from ._url import * from ._socket import* from ._exceptions import * +from ._ssl_compat import * __all__ = ["proxy_info", "connect", "read_headers"] diff --git a/websocket/_socket.py b/websocket/_socket.py index 233d076..315c808 100644 --- a/websocket/_socket.py +++ b/websocket/_socket.py @@ -25,6 +25,7 @@ import six from ._exceptions import * from ._utils import * +from ._ssl_compat import * DEFAULT_SOCKET_OPTION = [(socket.SOL_TCP, socket.TCP_NODELAY, 1)] if hasattr(socket, "SO_KEEPALIVE"): diff --git a/websocket/_ssl_compat.py b/websocket/_ssl_compat.py new file mode 100644 index 0000000..ae893fc --- /dev/null +++ b/websocket/_ssl_compat.py @@ -0,0 +1,41 @@ +""" +websocket - WebSocket client library for Python + +Copyright (C) 2010 Hiroki Ohtani(liris) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1335 USA + +""" + +__all__ = ["HAVE_SSL"] + +try: + import ssl + from ssl import SSLError + if hasattr(ssl, "match_hostname"): + from ssl import match_hostname + else: + from backports.ssl_match_hostname import match_hostname + __all__.append("match_hostname") + + HAVE_SSL = True +except ImportError: + # dummy class of SSLError for ssl none-support environment. + class SSLError(Exception): + pass + + __all__.append("SSLError") + HAVE_SSL = False