77
88This module provides access to the BSD socket interface.
99
10- See corresponding `CPython module <https://docs.python.org/3/library/socket.html >`_ for
11- comparison.
10+ See the corresponding `CPython module <https://docs.python.org/3/library/socket.html >`_
11+ for comparison.
1212
1313.. admonition :: Difference to CPython
1414 :class: attention
1515
1616 CPython used to have a ``socket.error `` exception which is now deprecated,
1717 and is an alias of OSError. In MicroPython, use OSError directly.
1818
19+ .. admonition :: Difference to CPython
20+ :class: attention
21+
22+ For efficiency and consistency, socket objects in MicroPython implement a stream
23+ (file-like) interface directly. In CPython, you need to convert a socket to
24+ a file-like object using ``makefile() `` method. This method is still supported
25+ by MicroPython (but is a no-op), so where compatibility with CPython matters,
26+ be sure to use it.
27+
1928Socket address format(s)
2029------------------------
2130
22- Functions below which expect a network address, accept it in the format of
31+ The functions below which expect a network address, accept it in the format of
2332`(ipv4_address, port) `, where `ipv4_address ` is a string with dot-notation numeric
2433IPv4 address, e.g. ``"8.8.8.8" ``, and port is integer port number in the range
25341-65535. Note the domain names are not accepted as `ipv4_address `, they should be
@@ -141,10 +150,19 @@ Methods
141150 .. method :: socket.send(bytes)
142151
143152 Send data to the socket. The socket must be connected to a remote socket.
153+ Returns number of bytes sent, which may be smaller than the length of data
154+ ("short write").
144155
145156 .. method :: socket.sendall(bytes)
146157
147- Send data to the socket. The socket must be connected to a remote socket.
158+ Send all data to the socket. The socket must be connected to a remote socket.
159+ Unlike ``send() ``, this method will try to send all of data, by sending data
160+ chunk by chunk consecutively.
161+
162+ The behavior of this method on non-blocking sockets is undefined. Due to this,
163+ on MicroPython, it's recommended to use ``write() `` method instead, which
164+ has the same "no short writes" policy for blocking sockets, and will return
165+ number of bytes sent on non-blocking sockets.
148166
149167 .. method :: socket.recv(bufsize)
150168
@@ -189,19 +207,22 @@ Methods
189207 Set blocking or non-blocking mode of the socket: if flag is false, the socket is set to non-blocking,
190208 else to blocking mode.
191209
192- This method is a shorthand for certain ``settimeout() `` calls::
210+ This method is a shorthand for certain ``settimeout() `` calls:
193211
194- sock.setblocking(True) is equivalent to sock.settimeout(None)
195- sock.setblocking(False) is equivalent to sock.settimeout(0.0)
212+ * `` sock.setblocking(True) `` is equivalent to `` sock.settimeout(None) ``
213+ * `` sock.setblocking(False) `` is equivalent to `` sock.settimeout(0) ``
196214
197- .. method :: socket.makefile(mode='rb')
215+ .. method :: socket.makefile(mode='rb', buffering=0 )
198216
199217 Return a file object associated with the socket. The exact returned type depends on the arguments
200- given to makefile(). The support is limited to binary modes only ('rb' and 'wb ').
218+ given to makefile(). The support is limited to binary modes only ('rb', 'wb', and 'rwb ').
201219 CPython's arguments: ``encoding ``, ``errors `` and ``newline `` are not supported.
202220
203- The socket must be in blocking mode; it can have a timeout, but the file object’s internal buffer
204- may end up in a inconsistent state if a timeout occurs.
221+ .. admonition :: Difference to CPython
222+ :class: attention
223+
224+ As MicroPython doesn't support buffered streams, values of ``buffering ``
225+ parameter is ignored and treated as if it was 0 (unbuffered).
205226
206227 .. admonition :: Difference to CPython
207228 :class: attention
@@ -213,12 +234,15 @@ Methods
213234
214235 Read up to size bytes from the socket. Return a bytes object. If ``size `` is not given, it
215236 reads all data available from the socket until ``EOF ``; as such the method will not return until
216- the socket is closed.
237+ the socket is closed. This function tries to read as much data as
238+ requested (no "short reads"). This may be not possible with
239+ non-blocking socket though, and then less data will be returned.
217240
218241 .. method :: socket.readinto(buf[, nbytes])
219242
220243 Read bytes into the ``buf ``. If ``nbytes `` is specified then read at most
221- that many bytes. Otherwise, read at most ``len(buf) `` bytes.
244+ that many bytes. Otherwise, read at most ``len(buf) `` bytes. Just as
245+ ``read() ``, this method follows "no short reads" policy.
222246
223247 Return value: number of bytes read and stored into ``buf ``.
224248
@@ -230,6 +254,9 @@ Methods
230254
231255 .. method :: socket.write(buf)
232256
233- Write the buffer of bytes to the socket.
257+ Write the buffer of bytes to the socket. This function will try to
258+ write all data to a socket (no "short writes"). This may be not possible
259+ with a non-blocking socket though, and returned value will be less than
260+ the length of ``buf ``.
234261
235262 Return value: number of bytes written.
0 commit comments