2021-08-12 04:59:29 +01:00
|
|
|
******************************
|
|
|
|
:mod:`socket` -- socket module
|
|
|
|
******************************
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
.. module:: socket
|
2014-10-31 01:37:19 +00:00
|
|
|
:synopsis: socket module
|
|
|
|
|
2017-07-02 13:37:31 +01:00
|
|
|
|see_cpython_module| :mod:`python:socket`.
|
2014-10-31 01:37:19 +00:00
|
|
|
|
2017-07-02 13:37:31 +01:00
|
|
|
This module provides access to the BSD socket interface.
|
2016-04-22 22:08:43 +01:00
|
|
|
|
2017-01-28 12:39:18 +00:00
|
|
|
.. admonition:: Difference to CPython
|
|
|
|
:class: attention
|
|
|
|
|
2017-12-04 16:36:20 +00:00
|
|
|
For efficiency and consistency, socket objects in MicroPython implement a `stream`
|
2017-01-28 12:39:18 +00:00
|
|
|
(file-like) interface directly. In CPython, you need to convert a socket to
|
2017-06-29 00:22:14 +01:00
|
|
|
a file-like object using `makefile()` method. This method is still supported
|
2017-01-28 12:39:18 +00:00
|
|
|
by MicroPython (but is a no-op), so where compatibility with CPython matters,
|
|
|
|
be sure to use it.
|
|
|
|
|
2016-04-22 22:31:05 +01:00
|
|
|
Socket address format(s)
|
|
|
|
------------------------
|
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
The native socket address format of the ``socket`` module is an opaque data type
|
2017-08-20 07:49:12 +01:00
|
|
|
returned by `getaddrinfo` function, which must be used to resolve textual address
|
|
|
|
(including numeric addresses)::
|
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
sockaddr = socket.getaddrinfo('www.micropython.org', 80)[0][-1]
|
2017-08-20 07:49:12 +01:00
|
|
|
# You must use getaddrinfo() even for numeric addresses
|
2021-08-12 04:59:29 +01:00
|
|
|
sockaddr = socket.getaddrinfo('127.0.0.1', 80)[0][-1]
|
2017-08-20 07:49:12 +01:00
|
|
|
# Now you can use that address
|
|
|
|
sock.connect(addr)
|
|
|
|
|
|
|
|
Using `getaddrinfo` is the most efficient (both in terms of memory and processing
|
|
|
|
power) and portable way to work with addresses.
|
|
|
|
|
|
|
|
However, ``socket`` module (note the difference with native MicroPython
|
2021-08-12 04:59:29 +01:00
|
|
|
``socket`` module described here) provides CPython-compatible way to specify
|
2017-08-20 07:49:12 +01:00
|
|
|
addresses using tuples, as described below. Note that depending on a
|
2020-06-04 02:38:45 +01:00
|
|
|
:term:`MicroPython port`, ``socket`` module can be builtin or need to be
|
|
|
|
installed from `micropython-lib` (as in the case of :term:`MicroPython Unix port`),
|
2017-08-20 07:49:12 +01:00
|
|
|
and some ports still accept only numeric addresses in the tuple format,
|
|
|
|
and require to use `getaddrinfo` function to resolve domain names.
|
|
|
|
|
|
|
|
Summing up:
|
|
|
|
|
|
|
|
* Always use `getaddrinfo` when writing portable applications.
|
|
|
|
* Tuple addresses described below can be used as a shortcut for
|
|
|
|
quick hacks and interactive use, if your port supports them.
|
|
|
|
|
|
|
|
Tuple address format for ``socket`` module:
|
|
|
|
|
|
|
|
* IPv4: *(ipv4_address, port)*, where *ipv4_address* is a string with
|
|
|
|
dot-notation numeric IPv4 address, e.g. ``"8.8.8.8"``, and *port* is and
|
|
|
|
integer port number in the range 1-65535. Note the domain names are not
|
|
|
|
accepted as *ipv4_address*, they should be resolved first using
|
2021-08-12 04:59:29 +01:00
|
|
|
`socket.getaddrinfo()`.
|
2017-08-20 07:49:12 +01:00
|
|
|
* IPv6: *(ipv6_address, port, flowinfo, scopeid)*, where *ipv6_address*
|
|
|
|
is a string with colon-notation numeric IPv6 address, e.g. ``"2001:db8::1"``,
|
|
|
|
and *port* is an integer port number in the range 1-65535. *flowinfo*
|
|
|
|
must be 0. *scopeid* is the interface scope identifier for link-local
|
|
|
|
addresses. Note the domain names are not accepted as *ipv6_address*,
|
2021-08-12 04:59:29 +01:00
|
|
|
they should be resolved first using `socket.getaddrinfo()`. Availability
|
2020-06-04 02:38:45 +01:00
|
|
|
of IPv6 support depends on a :term:`MicroPython port`.
|
2016-04-22 22:31:05 +01:00
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
Functions
|
|
|
|
---------
|
|
|
|
|
2020-01-11 06:44:17 +00:00
|
|
|
.. function:: getaddrinfo(host, port, af=0, type=0, proto=0, flags=0, /)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2020-06-04 02:38:45 +01:00
|
|
|
Translate the host/port argument into a sequence of 5-tuples that contain all the
|
2018-01-27 10:54:16 +00:00
|
|
|
necessary arguments for creating a socket connected to that service. Arguments
|
2018-06-27 06:18:46 +01:00
|
|
|
*af*, *type*, and *proto* (which have the same meaning as for the `socket()` function)
|
|
|
|
can be used to filter which kind of addresses are returned. If a parameter is not
|
2018-01-27 10:54:16 +00:00
|
|
|
specified or zero, all combinations of addresses can be returned (requiring
|
|
|
|
filtering on the user side).
|
|
|
|
|
|
|
|
The resulting list of 5-tuples has the following structure::
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
|
|
(family, type, proto, canonname, sockaddr)
|
|
|
|
|
|
|
|
The following example shows how to connect to a given url::
|
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
s = socket.socket()
|
2018-06-27 06:18:46 +01:00
|
|
|
# This assumes that if "type" is not specified, an address for
|
2018-01-27 10:54:16 +00:00
|
|
|
# SOCK_STREAM will be returned, which may be not true
|
2021-08-12 04:59:29 +01:00
|
|
|
s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][-1])
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2018-01-27 10:54:16 +00:00
|
|
|
Recommended use of filtering params::
|
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
s = socket.socket()
|
2018-06-27 06:18:46 +01:00
|
|
|
# Guaranteed to return an address which can be connect'ed to for
|
2018-01-27 10:54:16 +00:00
|
|
|
# stream operation.
|
2021-08-12 04:59:29 +01:00
|
|
|
s.connect(socket.getaddrinfo('www.micropython.org', 80, 0, SOCK_STREAM)[0][-1])
|
2018-01-27 10:54:16 +00:00
|
|
|
|
2017-01-28 10:55:51 +00:00
|
|
|
.. admonition:: Difference to CPython
|
|
|
|
:class: attention
|
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
CPython raises a ``socket.gaierror`` exception (`OSError` subclass) in case
|
2017-01-28 10:55:51 +00:00
|
|
|
of error in this function. MicroPython doesn't have ``socket.gaierror``
|
2017-06-29 00:22:14 +01:00
|
|
|
and raises OSError directly. Note that error numbers of `getaddrinfo()`
|
2017-01-28 10:55:51 +00:00
|
|
|
form a separate namespace and may not match error numbers from
|
2021-08-12 04:59:29 +01:00
|
|
|
the :mod:`errno` module. To distinguish `getaddrinfo()` errors, they are
|
2017-01-28 10:55:51 +00:00
|
|
|
represented by negative numbers, whereas standard system errors are
|
|
|
|
positive numbers (error numbers are accessible using ``e.args[0]`` property
|
|
|
|
from an exception object). The use of negative values is a provisional
|
|
|
|
detail which may change in the future.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-10-26 22:27:27 +01:00
|
|
|
.. function:: inet_ntop(af, bin_addr)
|
|
|
|
|
|
|
|
Convert a binary network address *bin_addr* of the given address family *af*
|
|
|
|
to a textual representation::
|
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
>>> socket.inet_ntop(socket.AF_INET, b"\x7f\0\0\1")
|
2017-10-26 22:27:27 +01:00
|
|
|
'127.0.0.1'
|
|
|
|
|
|
|
|
.. function:: inet_pton(af, txt_addr)
|
|
|
|
|
|
|
|
Convert a textual network address *txt_addr* of the given address family *af*
|
|
|
|
to a binary representation::
|
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
>>> socket.inet_pton(socket.AF_INET, "1.2.3.4")
|
2017-10-26 22:27:27 +01:00
|
|
|
b'\x01\x02\x03\x04'
|
|
|
|
|
2015-10-20 14:04:55 +01:00
|
|
|
Constants
|
|
|
|
---------
|
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
.. data:: AF_INET
|
|
|
|
AF_INET6
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2020-06-04 02:38:45 +01:00
|
|
|
Address family types. Availability depends on a particular :term:`MicroPython port`.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
.. data:: SOCK_STREAM
|
|
|
|
SOCK_DGRAM
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 11:46:58 +00:00
|
|
|
Socket types.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
.. data:: IPPROTO_UDP
|
|
|
|
IPPROTO_TCP
|
2016-06-07 15:41:21 +01:00
|
|
|
|
2020-06-04 02:38:45 +01:00
|
|
|
IP protocol numbers. Availability depends on a particular :term:`MicroPython port`.
|
2021-08-12 04:59:29 +01:00
|
|
|
Note that you don't need to specify these in a call to `socket.socket()`,
|
2017-10-26 12:00:16 +01:00
|
|
|
because `SOCK_STREAM` socket type automatically selects `IPPROTO_TCP`, and
|
|
|
|
`SOCK_DGRAM` - `IPPROTO_UDP`. Thus, the only real use of these constants
|
|
|
|
is as an argument to `setsockopt()`.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
.. data:: socket.SOL_*
|
2017-01-28 11:46:58 +00:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Socket option levels (an argument to `setsockopt()`). The exact
|
2020-06-04 02:38:45 +01:00
|
|
|
inventory depends on a :term:`MicroPython port`.
|
2017-01-28 11:46:58 +00:00
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
.. data:: socket.SO_*
|
2017-01-28 11:46:58 +00:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Socket options (an argument to `setsockopt()`). The exact
|
2020-06-04 02:38:45 +01:00
|
|
|
inventory depends on a :term:`MicroPython port`.
|
2017-01-28 11:46:58 +00:00
|
|
|
|
|
|
|
Constants specific to WiPy:
|
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
.. data:: IPPROTO_SEC
|
2017-01-28 11:46:58 +00:00
|
|
|
|
|
|
|
Special protocol value to create SSL-compatible socket.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
|
|
class socket
|
|
|
|
============
|
|
|
|
|
2021-07-26 23:11:11 +01:00
|
|
|
.. class:: socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP, /)
|
|
|
|
|
|
|
|
Create a new socket using the given address family, socket type and
|
|
|
|
protocol number. Note that specifying *proto* in most cases is not
|
|
|
|
required (and not recommended, as some MicroPython ports may omit
|
|
|
|
``IPPROTO_*`` constants). Instead, *type* argument will select needed
|
|
|
|
protocol automatically::
|
|
|
|
|
|
|
|
# Create STREAM TCP socket
|
|
|
|
socket(AF_INET, SOCK_STREAM)
|
|
|
|
# Create DGRAM UDP socket
|
|
|
|
socket(AF_INET, SOCK_DGRAM)
|
|
|
|
|
2015-10-20 14:04:55 +01:00
|
|
|
Methods
|
|
|
|
-------
|
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
.. method:: socket.close()
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Mark the socket closed and release all resources. Once that happens, all future operations
|
|
|
|
on the socket object will fail. The remote end will receive EOF indication if
|
|
|
|
supported by protocol.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2020-06-04 02:38:45 +01:00
|
|
|
Sockets are automatically closed when they are garbage-collected, but it is recommended
|
2017-06-29 00:22:14 +01:00
|
|
|
to `close()` them explicitly as soon you finished working with them.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.bind(address)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Bind the socket to *address*. The socket must not already be bound.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.listen([backlog])
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Enable a server to accept connections. If *backlog* is specified, it must be at least 0
|
2017-01-28 12:49:54 +00:00
|
|
|
(if it's lower, it will be set to 0); and specifies the number of unaccepted connections
|
|
|
|
that the system will allow before refusing new connections. If not specified, a default
|
|
|
|
reasonable value is chosen.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.accept()
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Accept a connection. The socket must be bound to an address and listening for connections.
|
|
|
|
The return value is a pair (conn, address) where conn is a new socket object usable to send
|
|
|
|
and receive data on the connection, and address is the address bound to the socket on the
|
|
|
|
other end of the connection.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.connect(address)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Connect to a remote socket at *address*.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.send(bytes)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Send data to the socket. The socket must be connected to a remote socket.
|
|
|
|
Returns number of bytes sent, which may be smaller than the length of data
|
|
|
|
("short write").
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.sendall(bytes)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Send all data to the socket. The socket must be connected to a remote socket.
|
2017-06-29 00:22:14 +01:00
|
|
|
Unlike `send()`, this method will try to send all of data, by sending data
|
2017-01-28 12:49:54 +00:00
|
|
|
chunk by chunk consecutively.
|
2017-01-28 12:39:18 +00:00
|
|
|
|
2021-04-30 07:53:36 +01:00
|
|
|
The behaviour of this method on non-blocking sockets is undefined. Due to this,
|
2017-06-29 00:22:14 +01:00
|
|
|
on MicroPython, it's recommended to use `write()` method instead, which
|
2017-01-28 12:49:54 +00:00
|
|
|
has the same "no short writes" policy for blocking sockets, and will return
|
|
|
|
number of bytes sent on non-blocking sockets.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.recv(bufsize)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Receive data from the socket. The return value is a bytes object representing the data
|
|
|
|
received. The maximum amount of data to be received at once is specified by bufsize.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.sendto(bytes, address)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Send data to the socket. The socket should not be connected to a remote socket, since the
|
2017-06-29 00:22:14 +01:00
|
|
|
destination socket is specified by *address*.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.recvfrom(bufsize)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Receive data from the socket. The return value is a pair *(bytes, address)* where *bytes* is a
|
|
|
|
bytes object representing the data received and *address* is the address of the socket sending
|
2017-01-28 12:49:54 +00:00
|
|
|
the data.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.setsockopt(level, optname, value)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Set the value of the given socket option. The needed symbolic constants are defined in the
|
2017-06-29 00:22:14 +01:00
|
|
|
socket module (SO_* etc.). The *value* can be an integer or a bytes-like object representing
|
2017-01-28 12:49:54 +00:00
|
|
|
a buffer.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.settimeout(value)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-10-30 22:28:28 +00:00
|
|
|
**Note**: Not every port supports this method, see below.
|
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Set a timeout on blocking socket operations. The value argument can be a nonnegative floating
|
|
|
|
point number expressing seconds, or None. If a non-zero value is given, subsequent socket operations
|
2017-06-29 00:22:14 +01:00
|
|
|
will raise an `OSError` exception if the timeout period value has elapsed before the operation has
|
2017-01-28 12:49:54 +00:00
|
|
|
completed. If zero is given, the socket is put in non-blocking mode. If None is given, the socket
|
|
|
|
is put in blocking mode.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2020-06-04 02:38:45 +01:00
|
|
|
Not every :term:`MicroPython port` supports this method. A more portable and
|
2021-08-12 04:59:29 +01:00
|
|
|
generic solution is to use `select.poll` object. This allows to wait on
|
2017-10-30 22:28:28 +00:00
|
|
|
multiple objects at the same time (and not just on sockets, but on generic
|
2017-12-04 16:36:20 +00:00
|
|
|
`stream` objects which support polling). Example::
|
2017-10-30 22:28:28 +00:00
|
|
|
|
|
|
|
# Instead of:
|
|
|
|
s.settimeout(1.0) # time in seconds
|
|
|
|
s.read(10) # may timeout
|
|
|
|
|
|
|
|
# Use:
|
2021-08-12 04:59:29 +01:00
|
|
|
poller = select.poll()
|
|
|
|
poller.register(s, select.POLLIN)
|
2017-10-30 22:28:28 +00:00
|
|
|
res = poller.poll(1000) # time in milliseconds
|
|
|
|
if not res:
|
|
|
|
# s is still not ready for input, i.e. operation timed out
|
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. admonition:: Difference to CPython
|
|
|
|
:class: attention
|
2017-01-07 11:23:33 +00:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
CPython raises a ``socket.timeout`` exception in case of timeout,
|
2017-06-29 00:22:14 +01:00
|
|
|
which is an `OSError` subclass. MicroPython raises an OSError directly
|
2017-01-28 12:49:54 +00:00
|
|
|
instead. If you use ``except OSError:`` to catch the exception,
|
|
|
|
your code will work both in MicroPython and CPython.
|
2017-01-07 11:23:33 +00:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.setblocking(flag)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Set blocking or non-blocking mode of the socket: if flag is false, the socket is set to non-blocking,
|
|
|
|
else to blocking mode.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
This method is a shorthand for certain `settimeout()` calls:
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
* ``sock.setblocking(True)`` is equivalent to ``sock.settimeout(None)``
|
|
|
|
* ``sock.setblocking(False)`` is equivalent to ``sock.settimeout(0)``
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2020-01-11 06:44:17 +00:00
|
|
|
.. method:: socket.makefile(mode='rb', buffering=0, /)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Return a file object associated with the socket. The exact returned type depends on the arguments
|
|
|
|
given to makefile(). The support is limited to binary modes only ('rb', 'wb', and 'rwb').
|
2017-06-29 00:22:14 +01:00
|
|
|
CPython's arguments: *encoding*, *errors* and *newline* are not supported.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. admonition:: Difference to CPython
|
|
|
|
:class: attention
|
2017-01-28 12:39:18 +00:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
As MicroPython doesn't support buffered streams, values of *buffering*
|
2017-01-28 12:49:54 +00:00
|
|
|
parameter is ignored and treated as if it was 0 (unbuffered).
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. admonition:: Difference to CPython
|
|
|
|
:class: attention
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Closing the file object returned by makefile() WILL close the
|
|
|
|
original socket as well.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.read([size])
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Read up to size bytes from the socket. Return a bytes object. If *size* is not given, it
|
|
|
|
reads all data available from the socket until EOF; as such the method will not return until
|
2017-01-28 12:49:54 +00:00
|
|
|
the socket is closed. This function tries to read as much data as
|
|
|
|
requested (no "short reads"). This may be not possible with
|
|
|
|
non-blocking socket though, and then less data will be returned.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.readinto(buf[, nbytes])
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Read bytes into the *buf*. If *nbytes* is specified then read at most
|
|
|
|
that many bytes. Otherwise, read at most *len(buf)* bytes. Just as
|
|
|
|
`read()`, this method follows "no short reads" policy.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-06-29 00:22:14 +01:00
|
|
|
Return value: number of bytes read and stored into *buf*.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.readline()
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Read a line, ending in a newline character.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Return value: the line read.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
.. method:: socket.write(buf)
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Write the buffer of bytes to the socket. This function will try to
|
|
|
|
write all data to a socket (no "short writes"). This may be not possible
|
|
|
|
with a non-blocking socket though, and returned value will be less than
|
2017-06-29 00:22:14 +01:00
|
|
|
the length of *buf*.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
2017-01-28 12:49:54 +00:00
|
|
|
Return value: number of bytes written.
|
2017-08-20 07:04:48 +01:00
|
|
|
|
2021-08-12 04:59:29 +01:00
|
|
|
.. exception:: socket.error
|
2017-08-20 07:04:48 +01:00
|
|
|
|
|
|
|
MicroPython does NOT have this exception.
|
|
|
|
|
|
|
|
.. admonition:: Difference to CPython
|
|
|
|
:class: attention
|
|
|
|
|
|
|
|
CPython used to have a ``socket.error`` exception which is now deprecated,
|
|
|
|
and is an alias of `OSError`. In MicroPython, use `OSError` directly.
|