You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
779 B

4 years ago
  1. import ssl
  2. import sys
  3. import idna
  4. __version__ = '1.1.0'
  5. real_match_hostname = ssl.match_hostname
  6. PY_370 = sys.version_info >= (3, 7, 0)
  7. def patched_match_hostname(cert, hostname):
  8. try:
  9. hostname = idna.encode(hostname, uts46=True).decode('ascii')
  10. except UnicodeError:
  11. hostname = hostname.encode('idna').decode('ascii')
  12. return real_match_hostname(cert, hostname)
  13. def patch_match_hostname():
  14. if PY_370:
  15. return
  16. if hasattr(ssl.match_hostname, 'patched'):
  17. return
  18. ssl.match_hostname = patched_match_hostname
  19. ssl.match_hostname.patched = True
  20. def reset_match_hostname():
  21. if PY_370:
  22. return
  23. if not hasattr(ssl.match_hostname, 'patched'):
  24. return
  25. ssl.match_hostname = real_match_hostname