|
| 1 | +******************************* |
1 | 2 | :mod:`usocket` -- socket module |
2 | | -=============================== |
| 3 | +******************************* |
3 | 4 |
|
4 | 5 | .. module:: usocket |
5 | 6 | :synopsis: socket module |
6 | 7 |
|
7 | | -Socket functionality. |
| 8 | +This module provides access to the BSD socket interface. |
8 | 9 |
|
9 | 10 | Functions |
10 | 11 | --------- |
11 | 12 |
|
12 | | -.. function:: getaddrinfo(host, port) |
| 13 | +.. function:: socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) |
13 | 14 |
|
| 15 | + Create a new socket using the given address family, socket type and protocol number. |
14 | 16 |
|
15 | | -.. function:: socket(family=AF_INET, type=SOCK_STREAM, fileno=-1) |
| 17 | + .. only:: port_wipy |
16 | 18 |
|
17 | | - Create a socket. |
| 19 | + .. note:: |
| 20 | + |
| 21 | + SSL sockets need to be created the following way before wrapping them with |
| 22 | + ``ssl.wrap_socket``:: |
| 23 | + |
| 24 | + import socket |
| 25 | + import ssl |
| 26 | + s = socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC) |
| 27 | + ss = ssl.wrap_socket(s) |
| 28 | + |
| 29 | +.. function:: socket.getaddrinfo(host, port) |
| 30 | + |
| 31 | + Translate the host/port argument into a sequence of 5-tuples that contain all the |
| 32 | + necessary arguments for creating a socket connected to that service. The list of |
| 33 | + 5-tuples has following structure:: |
| 34 | + |
| 35 | + (family, type, proto, canonname, sockaddr) |
| 36 | + |
| 37 | + The following example shows how to connect to a given url:: |
| 38 | + |
| 39 | + s = socket.socket() |
| 40 | + s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][4]) |
| 41 | + |
| 42 | +Exceptions |
| 43 | +---------- |
| 44 | + |
| 45 | +.. data:: socket.error |
| 46 | +.. data:: socket.timeout |
| 47 | + |
| 48 | +Constants |
| 49 | +--------- |
| 50 | + |
| 51 | +.. data:: socket.AF_INET |
| 52 | + |
| 53 | + family types |
| 54 | + |
| 55 | +.. data:: socket.SOCK_STREAM |
| 56 | +.. data:: socket.SOCK_DGRAM |
| 57 | + |
| 58 | + socket types |
| 59 | + |
| 60 | +.. data:: socket.IPPROTO_UDP |
| 61 | +.. data:: socket.IPPROTO_TCP |
| 62 | +.. data:: socket.IPPROTO_SEC |
| 63 | + |
| 64 | + protocol numbers |
| 65 | + |
| 66 | +class socket |
| 67 | +============ |
| 68 | + |
| 69 | +Methods |
| 70 | +------- |
| 71 | + |
| 72 | + .. method:: socket.close |
| 73 | + |
| 74 | + Mark the socket closed. Once that happens, all future operations on the socket |
| 75 | + object will fail. The remote end will receive no more data (after queued data is flushed). |
| 76 | + |
| 77 | + Sockets are automatically closed when they are garbage-collected, but it is recommended |
| 78 | + to close() them explicitly, or to use a with statement around them. |
| 79 | + |
| 80 | + .. method:: socket.bind(address) |
| 81 | + |
| 82 | + Bind the socket to address. The socket must not already be bound. The format of ``address`` |
| 83 | + is: ``(ipv4 address, port)`` |
| 84 | + |
| 85 | + .. method:: socket.listen([backlog]) |
| 86 | + |
| 87 | + Enable a server to accept connections. If backlog is specified, it must be at least 0 |
| 88 | + (if it's lower, it will be set to 0); and specifies the number of unaccepted connections |
| 89 | + tha the system will allow before refusing new connections. If not specified, a default |
| 90 | + reasonable value is chosen. |
| 91 | + |
| 92 | + .. method:: socket.accept() |
| 93 | + |
| 94 | + Accept a connection. The socket must be bound to an address and listening for connections. |
| 95 | + The return value is a pair (conn, address) where conn is a new socket object usable to send |
| 96 | + and receive data on the connection, and address is the address bound to the socket on the |
| 97 | + other end of the connection. |
| 98 | + |
| 99 | + .. method:: socket.connect(address) |
| 100 | + |
| 101 | + Connect to a remote socket at address. The format of address is: ``(ipv4 address, port)`` |
| 102 | + |
| 103 | + .. method:: socket.send(bytes) |
| 104 | + |
| 105 | + Send data to the socket. The socket must be connected to a remote socket. |
| 106 | + |
| 107 | + .. method:: socket.sendall(bytes) |
| 108 | + |
| 109 | + Send data to the socket. The socket must be connected to a remote socket. |
| 110 | + |
| 111 | + .. method:: socket.recv(bufsize) |
| 112 | + |
| 113 | + Receive data from the socket. The return value is a bytes object representing the data |
| 114 | + received. The maximum amount of data to be received at once is specified by bufsize. |
| 115 | + |
| 116 | + .. method:: socket.sendto(bytes, address) |
| 117 | + |
| 118 | + Send data to the socket. The socket should not be connected to a remote socket, since the |
| 119 | + destination socket is specified by address. The ``address`` has the same format as the |
| 120 | + rest of the methods, see above. |
| 121 | + |
| 122 | + .. method:: socket.recvfrom(bufsize) |
| 123 | + |
| 124 | + Receive data from the socket. The return value is a pair (bytes, address) where bytes is a |
| 125 | + bytes object representing the data received and address is the address of the socket sending |
| 126 | + the data. |
| 127 | + |
| 128 | + .. method:: socket.setsockopt(level, optname, value) |
| 129 | + |
| 130 | + Set the value of the given socket option. The needed symbolic constants are defined in the |
| 131 | + socket module (SO_* etc.). The value can be an integer or a bytes-like object representing |
| 132 | + a buffer. |
| 133 | + |
| 134 | + .. method:: socket.settimeout(value) |
| 135 | + |
| 136 | + Set a timeout on blocking socket operations. The value argument can be a nonnegative floating |
| 137 | + point number expressing seconds, or None. If a non-zero value is given, subsequent socket operations |
| 138 | + will raise a timeout exception if the timeout period value has elapsed before the operation has |
| 139 | + completed. If zero is given, the socket is put in non-blocking mode. If None is given, the socket |
| 140 | + is put in blocking mode. |
| 141 | + |
| 142 | + .. method:: socket.setblocking(flag) |
| 143 | + |
| 144 | + Set blocking or non-blocking mode of the socket: if flag is false, the socket is set to non-blocking, |
| 145 | + else to blocking mode. |
| 146 | + |
| 147 | + This method is a shorthand for certain ``settimeout()`` calls:: |
| 148 | + |
| 149 | + sock.setblocking(True) is equivalent to sock.settimeout(None) |
| 150 | + sock.setblocking(False) is equivalent to sock.settimeout(0.0) |
| 151 | + |
| 152 | + .. method:: socket.makefile(mode='rb') |
| 153 | + |
| 154 | + Return a file object associated with the socket. The exact returned type depends on the arguments |
| 155 | + given to makefile(). The support is limited to binary modes only ('rb' and 'wb'). |
| 156 | + CPython's arguments: ``encoding``, ``errors`` and ``newline`` are not supported. |
| 157 | + |
| 158 | + The socket must be in blocking mode; it can have a timeout, but the file object’s internal buffer |
| 159 | + may end up in a inconsistent state if a timeout occurs. |
| 160 | + |
| 161 | + .. note:: |
| 162 | + |
| 163 | + **CPython difference:** closing the file object returned by makefile() WILL close the |
| 164 | + original socket as well. |
| 165 | + |
| 166 | + .. method:: socket.read(size) |
| 167 | + |
| 168 | + Read up to size bytes from the socket. Return a bytes object. If ``size`` is not given, it |
| 169 | + behaves just like ``socket.readall()``, see below. |
| 170 | + |
| 171 | + .. method:: socket.readall() |
| 172 | + |
| 173 | + Read all data available from the socket until ``EOF``. This function will not return until |
| 174 | + the socket is closed. |
| 175 | + |
| 176 | + .. method:: socket.readinto(buf[, nbytes]) |
| 177 | + |
| 178 | + Read bytes into the ``buf``. If ``nbytes`` is specified then read at most |
| 179 | + that many bytes. Otherwise, read at most ``len(buf)`` bytes. |
| 180 | + |
| 181 | + Return value: number of bytes read and stored into ``buf``. |
| 182 | + |
| 183 | + .. method:: socket.readline() |
| 184 | + |
| 185 | + Read a line, ending in a newline character. |
| 186 | + |
| 187 | + Return value: the line read. |
| 188 | + |
| 189 | + .. method:: socket.write(buf) |
| 190 | + |
| 191 | + Write the buffer of bytes to the socket. |
| 192 | + |
| 193 | + Return value: number of bytes written. |
0 commit comments