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