Ameba MicroPython: [RTL8722CSM] [RTL8722DM] Socket - 从HTTP网站获取信息

材料准备

  • Ameba x 1

范例说明

创建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')
Copyrights ©瑞晟微电子(苏州)有限公司 2021. All rights reserved.
请先确认已安装QQ通讯软体