Tiny web page server
Serve a small web page from the board. Connect to Wi-Fi first (see wifi.py), then open the printed address in a browser on the same network.
A starter program from the MantisIDE Full IDE (Tools › Examples). Press Open in Full IDE to run it on your board, change it, and save it as your own.
# Serve a small web page from the board. Connect to Wi-Fi first (see wifi.py),
# then open the printed address in a browser on the same network.
import socket, network
ip = network.WLAN(network.STA_IF).ifconfig()[0]
server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("0.0.0.0", 80))
server.listen(1)
print("Open http://%s/" % ip)
count = 0
while True:
client, addr = server.accept()
client.recv(1024)
count += 1
page = "<h1>Hello from MicroPython</h1><p>Visits: %d</p>" % count
client.send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" + page)
client.close()
Comments