[RTL8722CSM] [RTL8722DM] Socket - Get information from HTTP website

Materials

  • AmebaD[AMB21 / AMB22] x 1

Steps

With socket created, we can visit an HTTP website and get information from it.

Copy and paste the following code into REPL under paste mode.

 1import socket
 2from wireless import WLAN
 3wifi = WLAN(mode = WLAN.STA)
 4wifi.connect(ssid = "YourWiFiSSID", pswd = "YourPassword") # change the ssid and pswd to yours
 5def http_get(url):
 6     _, _, host, path = url.split('/', 3)
 7     c = socket.SOCK()
 8     # We are visiting MicroPython official website's test page
 9     c.connect(host, 80)
10     c.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
11     while True:
12             data = c.recv(100)
13             if data:
14             print(str(data,'utf8'), end='')
15             else:
16             break
17http_get('http://micropython.org/ks/test.html')