Ameba MicroPython: [RTL8722CSM] [RTL8722DM] Socket -Get information from HTTP website

Materials

  • Ameba 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.

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')
Copyrights ©瑞晟微电子(苏州)有限公司 2021. All rights reserved.
Please confirm that QQ communication software is installed