2.3 Socket Module API Manual#
1. Overview#
This module encapsulates the socket library, allowing users to develop network applications by calling the socket library.
2. Example#
# Configure TCP/UDP socket debugging tool
import socket
import time
PORT = 60000
def client():
# Get IP address and port number
ai = socket.getaddrinfo("10.100.228.5", PORT)
# ai = socket.getaddrinfo("10.10.1.94", PORT)
print("Address info:", ai)
addr = ai[0][-1]
print("Connecting to address:", addr)
# Create socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
# Connect to the specified address
s.connect(addr)
for i in range(10):
msg = "K230 TCP client test message {0} \r\n".format(i)
print(msg)
# Send string data
print(s.write(msg))
time.sleep(0.2)
# Close the socket after a 1-second delay
time.sleep(1)
s.close()
print("End")
# Run the client program
client()
3. API Definition#
For more details, see Micropython socket module.
3.1 Constructor#
class socket.socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP)
Creates a new socket object using the specified address family (
af), socket type (type), and protocol (proto). Typically, there is no need to explicitly specify theprotoparameter, as MicroPython will automatically select the appropriate protocol type based ontype. Examples:Create a TCP stream socket:
socket(AF_INET, SOCK_STREAM)Create a UDP datagram socket:
socket(AF_INET, SOCK_DGRAM)
3.2 Methods#
socket.close()
Closes the socket and releases its resources. After closing, all operations on the socket object will fail. If supported by the protocol, the remote end will receive an EOF indication. Although sockets are automatically closed when garbage collected, it is recommended to explicitly call the
close()method after use.socket.bind(address)
Binds the socket to the specified IP address and port. Ensure the socket is not already bound before calling.
socket.listen([backlog])
Makes the server socket start listening for incoming connection requests.
backlogspecifies the maximum number of queued connections, with a minimum of 0 (values less than 0 are treated as 0). If not specified, the system default is used.socket.accept()
Accepts a client connection. This method returns
(conn, address), whereconnis a new socket object for sending and receiving data on the connection, andaddressis the client’s address.socket.connect(address)
Connects to the specified server socket address.
socket.send(bytes)
Sends data to the socket, which must be connected. Returns the number of bytes sent, which may be less than the total length of the data (a “short write”).
socket.sendall(bytes)
Sends all data to the socket, which must be connected. Unlike
send(), this method attempts to send all data continuously until complete. This method’s behavior is undefined for non-blocking sockets, so it is recommended to use thewrite()method in MicroPython.socket.recv(bufsize)
Receives data from the socket, returning the received data as a bytes object.
bufsizespecifies the maximum number of bytes to receive at once.socket.sendto(bytes, address)
Sends data to an unconnected socket, with the destination address specified by
address.socket.recvfrom(bufsize)
Receives data from an unconnected socket, returning
(bytes, address), wherebytesis the received data andaddressis the source address.socket.setsockopt(level, optname, value)
Sets a socket option.
valuecan be an integer or a bytes-like object.socket.settimeout(value)
Sets the timeout for socket operations (in seconds).
valuecan be a positive number, zero, orNone. If a timeout occurs, anOSErroris raised. For non-blocking mode, setvalueto zero; for blocking mode, set toNone.socket.setblocking(flag)
Sets the socket’s blocking mode. When
flagisFalse, the socket is in non-blocking mode; whenTrue, it is in blocking mode.socket.makefile(mode=’rb’, buffering=0)
Returns a file object associated with the socket. Only binary modes (such as
rb,wb, andrwb) are supported, and thebufferingparameter is ignored in MicroPython.socket.read([size])
Reads data from the socket, returning a bytes object. If
sizeis not specified, reads all available data until EOF.socket.readinto(buf[, nbytes])
Reads data into
buf. Ifnbytesis specified, reads up tonbytesbytes; otherwise, reads up tolen(buf)bytes.socket.readline()
Reads a line of data from the socket, returning a bytes object.
socket.write(buf)
Writes the data in
bufto the socket. Attempts to write all data, but for non-blocking sockets, may write only part of the data.socket.error
In MicroPython, the
socket.errorexception is not defined; useOSErrordirectly.