材料准备
范例说明
创建socket后,我们可以访问HTTP网站并从中获取信息。在粘贴模式下,将以下代码复制并粘贴到REPL中。
import socket
from wireless import WLAN
wifi = WLAN(mode = WLAN.STA)
wifi.connect(ssid = "YourWiFiSSID", pswd = "YourPassword") # change the ssid and pswd to yours
def http_get(url):
_, _, host, path = url.split('/', 3)
c = socket.SOCK()
# We are visiting MicroPython official website's test page
c.connect(host, 80)
c.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
data = c.recv(100)
if data:
print(str(data,'utf8'), end='')
else:
break
http_get('http://micropython.org/ks/test.html')