例程讲解14-http_client http客户端

# 简单的HTTPS客户端示例。

import network, usocket

# AP info
SSID='' # Network SSID
KEY=''  # Network key

PORT = 80
HOST = "www.google.com"

# 初始化wlan模块并连接到网络
print("Trying to connect... (may take a while)...")

wlan = network.WINC()
wlan.connect(SSID, key=KEY, security=wlan.WPA_PSK)

# 我们现在应该有一个通过DHCP的有效IP了
print(wlan.ifconfig())

# 通过DNS获取addr信息
addr = usocket.getaddrinfo(HOST, PORT)[0][4]
print(addr)

# 创建一个新的套接字并连接到addr
client = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
client.connect(addr)

# 设置超时
client.settimeout(3.0)

# 发送HTTP请求和recv响应
client.send("GET / HTTP/1.1\r\nHost: %s\r\n\r\n"%(HOST))
print(client.recv(1024))

# 关闭套接字
client.close()

results matching ""

    No results matching ""