_ssl_compat.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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,
  15. Boston, MA 02110-1335 USA
  16. """
  17. __all__ = ["HAVE_SSL", "ssl", "SSLError", "SSLWantReadError", "SSLWantWriteError"]
  18. try:
  19. import ssl
  20. from ssl import SSLError
  21. from ssl import SSLWantReadError
  22. from ssl import SSLWantWriteError
  23. if hasattr(ssl, 'SSLContext') and hasattr(ssl.SSLContext, 'check_hostname'):
  24. HAVE_CONTEXT_CHECK_HOSTNAME = True
  25. else:
  26. HAVE_CONTEXT_CHECK_HOSTNAME = False
  27. if hasattr(ssl, "match_hostname"):
  28. from ssl import match_hostname
  29. else:
  30. from backports.ssl_match_hostname import match_hostname
  31. __all__.append("match_hostname")
  32. __all__.append("HAVE_CONTEXT_CHECK_HOSTNAME")
  33. HAVE_SSL = True
  34. except ImportError:
  35. # dummy class of SSLError for ssl none-support environment.
  36. class SSLError(Exception):
  37. pass
  38. class SSLWantReadError(Exception):
  39. pass
  40. class SSLWantWriteError(Exception):
  41. pass
  42. HAVE_SSL = False