_ssl_compat.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """
  2. websocket - WebSocket client library for Python
  3. Copyright (C) 2010 Hiroki Ohtani(liris)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. """
  16. __all__ = ["HAVE_SSL", "ssl", "SSLError", "SSLWantReadError", "SSLWantWriteError"]
  17. try:
  18. import ssl
  19. from ssl import SSLError
  20. from ssl import SSLWantReadError
  21. from ssl import SSLWantWriteError
  22. if hasattr(ssl, 'SSLContext') and hasattr(ssl.SSLContext, 'check_hostname'):
  23. HAVE_CONTEXT_CHECK_HOSTNAME = True
  24. else:
  25. HAVE_CONTEXT_CHECK_HOSTNAME = False
  26. if hasattr(ssl, "match_hostname"):
  27. from ssl import match_hostname
  28. else:
  29. from backports.ssl_match_hostname import match_hostname
  30. __all__.append("match_hostname")
  31. __all__.append("HAVE_CONTEXT_CHECK_HOSTNAME")
  32. HAVE_SSL = True
  33. except ImportError:
  34. # dummy class of SSLError for ssl none-support environment.
  35. class SSLError(Exception):
  36. pass
  37. class SSLWantReadError(Exception):
  38. pass
  39. class SSLWantWriteError(Exception):
  40. pass
  41. ssl = None
  42. HAVE_SSL = False