2015-10-20 14:04:55 +01:00
|
|
|
|
*******************************
|
2014-10-31 22:21:37 +00:00
|
|
|
|
:mod:`usocket` -- socket module
|
2015-10-20 14:04:55 +01:00
|
|
|
|
*******************************
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
|
|
|
|
.. module:: usocket
|
|
|
|
|
:synopsis: socket module
|
|
|
|
|
|
2015-10-20 14:04:55 +01:00
|
|
|
|
This module provides access to the BSD socket interface.
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
2016-04-22 22:08:43 +01:00
|
|
|
|
See corresponding `CPython module <https://docs.python.org/3/library/socket.html>`_ for
|
|
|
|
|
comparison.
|
|
|
|
|
|
2016-04-22 22:31:05 +01:00
|
|
|
|
Socket address format(s)
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
Functions below which expect a network address, accept it in the format of
|
|
|
|
|
`(ipv4_address, port)`, where `ipv4_address` is a string with dot-notation numeric
|
|
|
|
|
IPv4 address, e.g. ``"8.8.8.8"``, and port is integer port number in the range
|
|
|
|
|
1-65535. Note the domain names are not accepted as `ipv4_address`, they should be
|
|
|
|
|
resolved first using ``socket.getaddrinfo()``.
|
|
|
|
|
|
2014-10-31 01:37:19 +00:00
|
|
|
|
Functions
|
|
|
|
|
---------
|
|
|
|
|
|
2015-10-20 14:04:55 +01:00
|
|
|
|
.. function:: socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
2015-10-20 14:04:55 +01:00
|
|
|
|
Create a new socket using the given address family, socket type and protocol number.
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
2015-10-20 14:04:55 +01:00
|
|
|
|
.. only:: port_wipy
|
2014-10-31 01:37:19 +00:00
|
|
|
|
|
2015-10-20 14:04:55 +01:00
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
SSL sockets need to be created the following way before wrapping them with
|
|
|
|
|
``ssl.wrap_socket``::
|
|
|
|
|
|
|
|
|
|
import socket
|
|
|
|
|
import ssl
|
|
|
|
|
s = socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC)
|
|
|
|
|
ss = ssl.wrap_socket(s)
|
|
|
|
|
|
|
|
|
|
.. function:: socket.getaddrinfo(host, port)
|
|
|
|
|
|
|
|
|
|
Translate the host/port argument into a sequence of 5-tuples that contain all the
|
|
|
|
|
necessary arguments for creating a socket connected to that service. The list of
|
|
|
|
|
5-tuples has following structure::
|
|
|
|
|
|
|
|
|
|
(family, type, proto, canonname, sockaddr)
|
|
|
|
|
|
|
|
|
|
The following example shows how to connect to a given url::
|
|
|
|
|
|
|
|
|
|
s = socket.socket()
|
2016-05-02 22:48:04 +01:00
|
|
|
|
s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][-1])
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
2016-04-22 22:17:09 +01:00
|
|
|
|
.. only:: port_wipy
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
2016-04-22 22:17:09 +01:00
|
|
|
|
Exceptions
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
.. data:: socket.error
|
|
|
|
|
.. data:: socket.timeout
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
|
|
|
|
Constants
|
|
|
|
|
---------
|
|
|
|
|
|
|
|
|
|
.. data:: socket.AF_INET
|
|
|
|
|
|
|
|
|
|
family types
|
|
|
|
|
|
|
|
|
|
.. data:: socket.SOCK_STREAM
|
|
|
|
|
.. data:: socket.SOCK_DGRAM
|
|
|
|
|
|
|
|
|
|
socket types
|
|
|
|
|
|
|
|
|
|
.. data:: socket.IPPROTO_UDP
|
|
|
|
|
.. data:: socket.IPPROTO_TCP
|
2016-04-22 22:17:34 +01:00
|
|
|
|
.. only:: port_wipy
|
2016-06-07 15:41:21 +01:00
|
|
|
|
|
2016-04-22 22:17:34 +01:00
|
|
|
|
.. data:: socket.IPPROTO_SEC
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
2016-04-22 22:17:34 +01:00
|
|
|
|
protocol numbers
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
|
|
|
|
class socket
|
|
|
|
|
============
|
|
|
|
|
|
|
|
|
|
Methods
|
|
|
|
|
-------
|
|
|
|
|
|
|
|
|
|
.. method:: socket.close
|
|
|
|
|
|
|
|
|
|
Mark the socket closed. Once that happens, all future operations on the socket
|
|
|
|
|
object will fail. The remote end will receive no more data (after queued data is flushed).
|
|
|
|
|
|
|
|
|
|
Sockets are automatically closed when they are garbage-collected, but it is recommended
|
|
|
|
|
to close() them explicitly, or to use a with statement around them.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.bind(address)
|
|
|
|
|
|
2016-04-22 22:31:05 +01:00
|
|
|
|
Bind the socket to address. The socket must not already be bound.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
|
|
|
|
.. method:: socket.listen([backlog])
|
|
|
|
|
|
|
|
|
|
Enable a server to accept connections. If backlog is specified, it must be at least 0
|
|
|
|
|
(if it's lower, it will be set to 0); and specifies the number of unaccepted connections
|
|
|
|
|
tha the system will allow before refusing new connections. If not specified, a default
|
|
|
|
|
reasonable value is chosen.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.accept()
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.connect(address)
|
|
|
|
|
|
2016-04-22 22:31:05 +01:00
|
|
|
|
Connect to a remote socket at address.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
|
|
|
|
.. method:: socket.send(bytes)
|
|
|
|
|
|
|
|
|
|
Send data to the socket. The socket must be connected to a remote socket.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.sendall(bytes)
|
|
|
|
|
|
|
|
|
|
Send data to the socket. The socket must be connected to a remote socket.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.recv(bufsize)
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.sendto(bytes, address)
|
|
|
|
|
|
|
|
|
|
Send data to the socket. The socket should not be connected to a remote socket, since the
|
2016-04-22 22:31:05 +01:00
|
|
|
|
destination socket is specified by `address`.
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
|
|
|
|
.. method:: socket.recvfrom(bufsize)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
the data.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.setsockopt(level, optname, value)
|
|
|
|
|
|
|
|
|
|
Set the value of the given socket option. The needed symbolic constants are defined in the
|
|
|
|
|
socket module (SO_* etc.). The value can be an integer or a bytes-like object representing
|
|
|
|
|
a buffer.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.settimeout(value)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
will raise a timeout exception if the timeout period value has elapsed before the operation has
|
|
|
|
|
completed. If zero is given, the socket is put in non-blocking mode. If None is given, the socket
|
|
|
|
|
is put in blocking mode.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.setblocking(flag)
|
|
|
|
|
|
|
|
|
|
Set blocking or non-blocking mode of the socket: if flag is false, the socket is set to non-blocking,
|
|
|
|
|
else to blocking mode.
|
|
|
|
|
|
|
|
|
|
This method is a shorthand for certain ``settimeout()`` calls::
|
|
|
|
|
|
|
|
|
|
sock.setblocking(True) is equivalent to sock.settimeout(None)
|
|
|
|
|
sock.setblocking(False) is equivalent to sock.settimeout(0.0)
|
|
|
|
|
|
|
|
|
|
.. method:: socket.makefile(mode='rb')
|
|
|
|
|
|
|
|
|
|
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' and 'wb').
|
|
|
|
|
CPython's arguments: ``encoding``, ``errors`` and ``newline`` are not supported.
|
|
|
|
|
|
|
|
|
|
The socket must be in blocking mode; it can have a timeout, but the file object’s internal buffer
|
|
|
|
|
may end up in a inconsistent state if a timeout occurs.
|
|
|
|
|
|
2016-04-27 13:43:48 +01:00
|
|
|
|
.. admonition:: Difference to CPython
|
|
|
|
|
:class: attention
|
2015-10-20 14:04:55 +01:00
|
|
|
|
|
2016-04-27 13:43:48 +01:00
|
|
|
|
Closing the file object returned by makefile() WILL close the
|
2015-10-20 14:04:55 +01:00
|
|
|
|
original socket as well.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.read(size)
|
|
|
|
|
|
|
|
|
|
Read up to size bytes from the socket. Return a bytes object. If ``size`` is not given, it
|
|
|
|
|
behaves just like ``socket.readall()``, see below.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.readall()
|
|
|
|
|
|
|
|
|
|
Read all data available from the socket until ``EOF``. This function will not return until
|
|
|
|
|
the socket is closed.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.readinto(buf[, nbytes])
|
|
|
|
|
|
|
|
|
|
Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
|
|
|
|
|
that many bytes. Otherwise, read at most ``len(buf)`` bytes.
|
|
|
|
|
|
|
|
|
|
Return value: number of bytes read and stored into ``buf``.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.readline()
|
|
|
|
|
|
|
|
|
|
Read a line, ending in a newline character.
|
|
|
|
|
|
|
|
|
|
Return value: the line read.
|
|
|
|
|
|
|
|
|
|
.. method:: socket.write(buf)
|
|
|
|
|
|
|
|
|
|
Write the buffer of bytes to the socket.
|
|
|
|
|
|
|
|
|
|
Return value: number of bytes written.
|