import network
import socket as usocket
import ssl as ussl
import time
# Configuration du Wi-Fi
SSID = "Wokwi-GUEST"
PASSWORD = ""
def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
print("Connexion au Wi-Fi...")
while not wlan.isconnected():
time.sleep(0.5)
print("Connecté :", wlan.ifconfig())
def get_https_resource(host, port=443, path="/get"):
addr = usocket.getaddrinfo(host, port)[0][-1]
sock = usocket.socket()
sock.connect(addr)
# Socket sécurisé (sans vérification du certificat)
ssl_sock = ussl.wrap_socket(sock, server_hostname=host)
# Requête GET
request = "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n".format(path, host)
ssl_sock.write(request.encode())
print("=== Réponse reçue ===")
while True:
data = ssl_sock.read(1024)
if not data:
break
print(data.decode(), end='')
ssl_sock.close()
sock.close()
# Utilisation correcte
connect_wifi(SSID, PASSWORD)
time.sleep(1)
get_https_resource("httpbin.org", path="/get")