test_tls_sites.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. try:
  2. import usocket as _socket
  3. except:
  4. import _socket
  5. try:
  6. import ussl as ssl
  7. except:
  8. import ssl
  9. # CPython only supports server_hostname with SSLContext
  10. ssl = ssl.SSLContext()
  11. def test_one(site, opts):
  12. ai = _socket.getaddrinfo(site, 443)
  13. addr = ai[0][-1]
  14. s = _socket.socket()
  15. try:
  16. s.connect(addr)
  17. if "sni" in opts:
  18. s = ssl.wrap_socket(s, server_hostname=opts["host"])
  19. else:
  20. s = ssl.wrap_socket(s)
  21. s.write(b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % bytes(site, 'latin'))
  22. resp = s.read(4096)
  23. # print(resp)
  24. finally:
  25. s.close()
  26. SITES = [
  27. "google.com",
  28. "www.google.com",
  29. "api.telegram.org",
  30. {"host": "api.pushbullet.com", "sni": True},
  31. # "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com",
  32. {"host": "w9rybpfril.execute-api.ap-southeast-2.amazonaws.com", "sni": True},
  33. ]
  34. def main():
  35. for site in SITES:
  36. opts = {}
  37. if isinstance(site, dict):
  38. opts = site
  39. site = opts["host"]
  40. try:
  41. test_one(site, opts)
  42. print(site, "ok")
  43. except Exception as e:
  44. print(site, repr(e))
  45. main()