例程讲解14-http_client_ssl http客户端加密
import network
import socket
import ssl
import time
SSID = ""
KEY = ""
PORT = 443
HOST = "www.google.com"
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, KEY)
while not wlan.isconnected():
print('Trying to connect to "{:s}"...'.format(SSID))
time.sleep_ms(1000)
print("WiFi Connected ", wlan.ifconfig())
addr = socket.getaddrinfo(HOST, PORT)[0][4]
print(addr)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(addr)
client.settimeout(3.0)
client = ssl.wrap_socket(client, server_hostname=HOST)
request = "GET / HTTP/1.1\r\n"
request += "HOST: %s\r\n"
request += "User-Agent: Mozilla/5.0\r\n"
request += "Connection: keep-alive\r\n\r\n"
client.write(request % (HOST) + "\r\n")
response = client.read(1024)
for l in response.split(b"\r\n"):
print(l.decode())
client.close()