2022-08-18 07:57:45 +01:00
|
|
|
import socket, ssl, errno, sys, time, select
|
2020-04-02 18:01:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_one(site, opts):
|
2023-12-12 06:17:22 +00:00
|
|
|
ai = socket.getaddrinfo(site, 443, socket.AF_INET)
|
2020-04-02 18:01:16 +01:00
|
|
|
addr = ai[0][-1]
|
2023-12-12 06:17:22 +00:00
|
|
|
print(site)
|
2020-04-02 18:01:16 +01:00
|
|
|
|
|
|
|
# Connect the raw socket
|
2023-12-12 06:17:22 +00:00
|
|
|
s = socket.socket(socket.AF_INET)
|
2020-04-02 18:01:16 +01:00
|
|
|
s.setblocking(False)
|
|
|
|
try:
|
|
|
|
s.connect(addr)
|
|
|
|
raise OSError(-1, "connect blocks")
|
|
|
|
except OSError as e:
|
2021-04-22 10:32:21 +01:00
|
|
|
if e.errno != errno.EINPROGRESS:
|
2020-04-02 18:01:16 +01:00
|
|
|
raise
|
|
|
|
|
2023-12-12 06:17:22 +00:00
|
|
|
# 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
|
2020-04-02 18:01:16 +01:00
|
|
|
if sys.implementation.name != "micropython":
|
2023-12-12 06:17:22 +00:00
|
|
|
ssl_context.check_hostname = False
|
|
|
|
ssl_context.load_default_certs()
|
2020-04-02 18:01:16 +01:00
|
|
|
select.select([], [s], [])
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Wrap with SSL
|
|
|
|
try:
|
2023-12-12 06:17:22 +00:00
|
|
|
s = ssl_context.wrap_socket(s, do_handshake_on_connect=False)
|
2020-04-02 18:01:16 +01:00
|
|
|
except OSError as e:
|
2021-04-22 10:32:21 +01:00
|
|
|
if e.errno != errno.EINPROGRESS:
|
2020-04-02 18:01:16 +01:00
|
|
|
raise
|
|
|
|
print("wrapped")
|
|
|
|
|
|
|
|
# CPython needs to be told to do the handshake
|
|
|
|
if sys.implementation.name != "micropython":
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
s.do_handshake()
|
|
|
|
break
|
|
|
|
except ssl.SSLError as err:
|
|
|
|
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
|
|
|
|
select.select([s], [], [])
|
|
|
|
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
|
|
|
|
select.select([], [s], [])
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
time.sleep(0.1)
|
|
|
|
# print("shook hands")
|
|
|
|
|
|
|
|
# Write HTTP request
|
|
|
|
out = b"GET / HTTP/1.0\r\nHost: %s\r\n\r\n" % bytes(site, "latin")
|
|
|
|
while len(out) > 0:
|
|
|
|
n = s.write(out)
|
|
|
|
if n is None:
|
|
|
|
continue
|
|
|
|
if n > 0:
|
|
|
|
out = out[n:]
|
|
|
|
elif n == 0:
|
|
|
|
raise OSError(-1, "unexpected EOF in write")
|
|
|
|
print("wrote")
|
|
|
|
|
|
|
|
# Read response
|
|
|
|
resp = b""
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
b = s.read(128)
|
|
|
|
except OSError as err:
|
2021-04-22 10:32:21 +01:00
|
|
|
if err.errno == 2: # 2=ssl.SSL_ERROR_WANT_READ:
|
2020-04-02 18:01:16 +01:00
|
|
|
continue
|
|
|
|
raise
|
|
|
|
if b is None:
|
|
|
|
continue
|
|
|
|
if len(b) > 0:
|
|
|
|
if len(resp) < 1024:
|
|
|
|
resp += b
|
|
|
|
elif len(b) == 0:
|
|
|
|
break
|
|
|
|
print("read")
|
|
|
|
|
|
|
|
if resp[:7] != b"HTTP/1.":
|
|
|
|
raise ValueError("response doesn't start with HTTP/1.")
|
|
|
|
# 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",
|
|
|
|
{"host": "api.pushbullet.com", "sni": True},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
2023-12-12 06:17:22 +00:00
|
|
|
print(site, "error", e)
|
2020-04-02 18:01:16 +01:00
|
|
|
print("DONE")
|
|
|
|
|
|
|
|
|
|
|
|
main()
|