2023-12-12 06:17:22 +00:00
|
|
|
import sys
|
|
|
|
import select
|
2022-08-18 07:57:45 +01:00
|
|
|
import socket
|
|
|
|
import ssl
|
|
|
|
|
2017-06-20 23:58:36 +01:00
|
|
|
|
|
|
|
def test_one(site, opts):
|
2023-12-12 06:17:22 +00:00
|
|
|
ai = socket.getaddrinfo(site, 443, socket.AF_INET)
|
2017-06-20 23:58:36 +01:00
|
|
|
addr = ai[0][-1]
|
|
|
|
|
2023-12-12 06:17:22 +00:00
|
|
|
s = socket.socket(socket.AF_INET)
|
|
|
|
|
|
|
|
# Create SSLContext.
|
|
|
|
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|
|
|
|
|
|
|
# CPython compatibility:
|
|
|
|
# - disable check_hostname
|
|
|
|
# - load default system certificate chain
|
|
|
|
# - must wait for socket to be writable before calling wrap_socket
|
|
|
|
if sys.implementation.name != "micropython":
|
|
|
|
ssl_context.check_hostname = False
|
|
|
|
ssl_context.load_default_certs()
|
|
|
|
select.select([], [s], [])
|
2017-06-20 23:58:36 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
s.connect(addr)
|
|
|
|
|
|
|
|
if "sni" in opts:
|
2023-12-12 06:17:22 +00:00
|
|
|
s = ssl_context.wrap_socket(s, server_hostname=opts["host"])
|
2017-06-20 23:58:36 +01:00
|
|
|
else:
|
2023-12-12 06:17:22 +00:00
|
|
|
s = ssl_context.wrap_socket(s)
|
2017-06-20 23:58:36 +01:00
|
|
|
|
2017-10-26 02:29:24 +01:00
|
|
|
s.write(b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % bytes(site, "latin"))
|
2017-06-20 23:58:36 +01:00
|
|
|
resp = s.read(4096)
|
2020-04-02 18:01:16 +01:00
|
|
|
if resp[:7] != b"HTTP/1.":
|
|
|
|
raise ValueError("response doesn't start with HTTP/1.")
|
2017-06-20 23:58:36 +01:00
|
|
|
# print(resp)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
|
|
|
|
SITES = [
|
2023-12-12 06:17:22 +00:00
|
|
|
"www.github.com",
|
2020-04-02 18:01:16 +01:00
|
|
|
"micropython.org",
|
|
|
|
"pypi.org",
|
2017-10-26 02:29:24 +01:00
|
|
|
{"host": "api.pushbullet.com", "sni": True},
|
2017-06-20 23:58:36 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
for site in SITES:
|
|
|
|
opts = {}
|
|
|
|
if isinstance(site, dict):
|
|
|
|
opts = site
|
|
|
|
site = opts["host"]
|
|
|
|
|
|
|
|
try:
|
|
|
|
test_one(site, opts)
|
|
|
|
print(site, "ok")
|
|
|
|
except Exception as e:
|
2020-07-02 20:34:36 +01:00
|
|
|
print(site, e)
|
2017-06-20 23:58:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
main()
|