HTTP-Server Example Explanation#
Environment Preparation#
First, ensure that your CanMV development board is connected to a router or switch via the network port, and the router is working properly with internet access capability. This environment is a prerequisite for implementing HTTP requests.
Server Example Detailed Explanation#
Below is a simple HTTP server Python example based on the CanMV development board. The server listens on port 8081 and can respond to HTTP requests from clients.
Importing Necessary Modules#
import socket
import network
import time
By importing the socket, network, and time modules, the socket module is used for network communication, network manages network interfaces (such as LAN), and time provides time-related functions.
Defining Response Content#
CONTENT = b"""\
HTTP/1.0 200 OK
Hello #%d from k230 canmv MicroPython!
"""
Define a byte string CONTENT as the HTTP response body. %d is a counter placeholder, indicating the sequence number of each request.
Defining the Main Function#
def main(micropython_optimize=True):
# ...(subsequent code)
The main function is defined with the parameter micropython_optimize controlling whether to enable specific MicroPython optimization methods.
Configuring the Network Interface#
def network_use_wlan(is_wlan=True):
if is_wlan:
sta = network.WLAN(0)
sta.connect("TEST", "12345678")
print(sta.status())
while sta.ifconfig()[0] == '0.0.0.0':
os.exitpoint()
print(sta.ifconfig())
ip = sta.ifconfig()[0]
return ip
else:
a = network.LAN()
if not a.active():
raise RuntimeError("LAN interface is not active.")
a.ifconfig("dhcp")
print(a.ifconfig())
ip = a.ifconfig()[0]
return ip
This code selects the WLAN or LAN interface based on the is_wlan parameter. In WLAN mode, it connects to the specified Wi-Fi network, while in LAN mode, it obtains an IP address via DHCP. After retrieving and printing the network configuration, it returns the IP.
Creating and Configuring the Socket#
# Create socket object
s = socket.socket()
# Set socket option to allow address reuse
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# Bind to all network interfaces, listen on port 8081
ai = socket.getaddrinfo("0.0.0.0", 8081)
addr = ai[0][-1]
s.bind(addr)
# Start listening, maximum number of connections is 5
s.listen(5)
print("Listening, connect your browser to http://%s:8081/" % (network.LAN().ifconfig()[0]))
The code creates a socket and enables the SO_REUSEADDR option to allow port reuse. It binds the address and starts listening on port 8081, supporting up to 5 connection requests.
Handling Client Requests#
counter = 0
while True:
# Accept connection
res = s.accept()
client_sock = res[0]
client_addr = res[1]
print("Client address:", client_addr)
# Select different reading methods based on whether optimization is enabled
if not micropython_optimize:
# Use streaming interface (for CPython)
client_stream = client_sock.makefile("rwb")
else:
# Use MicroPython-specific interface
client_stream = client_sock
# Read request content
# ...
# Send response
client_stream.write(CONTENT % counter)
# Close connection
client_stream.close()
counter += 1
time.sleep(2)
if counter > 0:
print("HTTP server exit!")
s.close()
break
The server’s main loop accepts client connections, selects different reading methods to handle requests, sends a response with a counter, and closes the connection. After the connection is closed, it waits 2 seconds before entering the next loop.
Complete Example#
import socket
import network
import time, os
CONTENT = b"""\
HTTP/1.0 200 OK
Hello #%d from k230 canmv MicroPython!
"""
def network_use_wlan(is_wlan=True):
if is_wlan:
sta = network.WLAN(0)
sta.connect("TEST", "12345678")
print(sta.status())
while sta.ifconfig()[0] == '0.0.0.0':
os.exitpoint()
print(sta.ifconfig())
ip = sta.ifconfig()[0]
return ip
else:
a = network.LAN()
if not a.active():
raise RuntimeError("LAN interface is not active.")
a.ifconfig("dhcp")
print(a.ifconfig())
ip = a.ifconfig()[0]
return ip
def main(micropython_optimize=True):
ip = network_use_wlan(True)
s = socket.socket()
ai = socket.getaddrinfo("0.0.0.0", 8081)
addr = ai[0][-1]
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print("Listening, connect your browser to http://%s:8081/" % (ip))
counter = 0
while True:
res = s.accept()
client_sock = res[0]
client_addr = res[1]
print("Client address:", client_addr)
client_sock.setblocking(True)
client_stream = client_sock if micropython_optimize else client_sock.makefile("rwb")
while True:
h = client_stream.read()
if h is None:
continue
print(h)
if h.endswith(b'\r\n\r\n'):
break
os.exitpoint()
client_stream.write(CONTENT % counter)
client_stream.close()
counter += 1
time.sleep(2)
if counter > 0:
print("http server exit!")
s.close()
break
main()
For specific interface definitions, please refer to socket、network.
Example Phenomena and Operation Instructions#
After running this example in CanMV IDE K230, the IDE serial terminal will display the following information:

Copy the URL from the terminal and access it in a browser to view the server’s response content:

