2022-08-19 13:58:37 +01:00
|
|
|
import socket
|
|
|
|
import ssl
|
2016-04-02 21:19:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main(use_stream=True):
|
2022-08-19 13:58:37 +01:00
|
|
|
s = socket.socket()
|
2016-04-02 21:19:03 +01:00
|
|
|
|
2022-08-19 13:58:37 +01:00
|
|
|
ai = socket.getaddrinfo("google.com", 443)
|
2016-04-02 21:19:03 +01:00
|
|
|
print("Address infos:", ai)
|
2016-05-02 22:45:37 +01:00
|
|
|
addr = ai[0][-1]
|
2016-04-02 21:19:03 +01:00
|
|
|
|
|
|
|
print("Connect address:", addr)
|
|
|
|
s.connect(addr)
|
|
|
|
|
|
|
|
s = ssl.wrap_socket(s)
|
|
|
|
print(s)
|
|
|
|
|
|
|
|
if use_stream:
|
|
|
|
# Both CPython and MicroPython SSLSocket objects support read() and
|
|
|
|
# write() methods.
|
2016-10-09 17:36:04 +01:00
|
|
|
s.write(b"GET / HTTP/1.0\r\n\r\n")
|
2016-04-02 21:19:03 +01:00
|
|
|
print(s.read(4096))
|
|
|
|
else:
|
|
|
|
# MicroPython SSLSocket objects implement only stream interface, not
|
|
|
|
# socket interface
|
2016-10-09 17:36:04 +01:00
|
|
|
s.send(b"GET / HTTP/1.0\r\n\r\n")
|
2016-04-02 21:19:03 +01:00
|
|
|
print(s.recv(4096))
|
|
|
|
|
2016-07-22 22:41:19 +01:00
|
|
|
s.close()
|
|
|
|
|
2016-04-02 21:19:03 +01:00
|
|
|
|
|
|
|
main()
|