@@ -14,34 +14,34 @@ for comparison.
14
14
:class: attention
15
15
16
16
CPython used to have a ``socket.error `` exception which is now deprecated,
17
- and is an alias of OSError. In MicroPython, use OSError directly.
17
+ and is an alias of ` OSError ` . In MicroPython, use ` OSError ` directly.
18
18
19
19
.. admonition :: Difference to CPython
20
20
:class: attention
21
21
22
22
For efficiency and consistency, socket objects in MicroPython implement a stream
23
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
24
+ a file-like object using `makefile() ` method. This method is still supported
25
25
by MicroPython (but is a no-op), so where compatibility with CPython matters,
26
26
be sure to use it.
27
27
28
28
Socket address format(s)
29
29
------------------------
30
30
31
31
The functions below which expect a network address, accept it in the format of
32
- ` (ipv4_address, port) ` , where ` ipv4_address ` is a string with dot-notation numeric
32
+ * (ipv4_address, port) * , where * ipv4_address * is a string with dot-notation numeric
33
33
IPv4 address, e.g. ``"8.8.8.8" ``, and port is integer port number in the range
34
- 1-65535. Note the domain names are not accepted as ` ipv4_address ` , they should be
35
- resolved first using `` socket .getaddrinfo()` `.
34
+ 1-65535. Note the domain names are not accepted as * ipv4_address * , they should be
35
+ resolved first using `usocket .getaddrinfo() `.
36
36
37
37
Functions
38
38
---------
39
39
40
- .. function :: socket.socket(socket. AF_INET, socket. SOCK_STREAM, socket. IPPROTO_TCP)
40
+ .. function :: socket(af= AF_INET, type= SOCK_STREAM, proto= IPPROTO_TCP)
41
41
42
42
Create a new socket using the given address family, socket type and protocol number.
43
43
44
- .. function :: socket. getaddrinfo(host, port)
44
+ .. function :: getaddrinfo(host, port)
45
45
46
46
Translate the host/port argument into a sequence of 5-tuples that contain all the
47
47
necessary arguments for creating a socket connected to that service. The list of
@@ -57,11 +57,11 @@ Functions
57
57
.. admonition :: Difference to CPython
58
58
:class: attention
59
59
60
- CPython raises a ``socket.gaierror `` exception (OSError subclass) in case
60
+ CPython raises a ``socket.gaierror `` exception (` OSError ` subclass) in case
61
61
of error in this function. MicroPython doesn't have ``socket.gaierror ``
62
- and raises OSError directly. Note that error numbers of `` getaddrinfo() ` `
62
+ and raises OSError directly. Note that error numbers of `getaddrinfo() `
63
63
form a separate namespace and may not match error numbers from
64
- `` uerrno `` module. To distinguish `` getaddrinfo() ` ` errors, they are
64
+ `uerrno ` module. To distinguish `getaddrinfo() ` errors, they are
65
65
represented by negative numbers, whereas standard system errors are
66
66
positive numbers (error numbers are accessible using ``e.args[0] `` property
67
67
from an exception object). The use of negative values is a provisional
@@ -70,32 +70,34 @@ Functions
70
70
Constants
71
71
---------
72
72
73
- .. data :: socket. AF_INET
74
- socket. AF_INET6
73
+ .. data :: AF_INET
74
+ AF_INET6
75
75
76
76
Address family types. Availability depends on a particular board.
77
77
78
- .. data :: socket. SOCK_STREAM
79
- socket. SOCK_DGRAM
78
+ .. data :: SOCK_STREAM
79
+ SOCK_DGRAM
80
80
81
81
Socket types.
82
82
83
- .. data :: socket. IPPROTO_UDP
84
- socket. IPPROTO_TCP
83
+ .. data :: IPPROTO_UDP
84
+ IPPROTO_TCP
85
85
86
86
IP protocol numbers.
87
87
88
- .. data :: socket .SOL_*
88
+ .. data :: usocket .SOL_*
89
89
90
- Socket option levels (an argument to ``setsockopt() ``). The exact inventory depends on a board.
90
+ Socket option levels (an argument to `setsockopt() `). The exact
91
+ inventory depends on a MicroPython port.
91
92
92
- .. data :: socket .SO_*
93
+ .. data :: usocket .SO_*
93
94
94
- Socket options (an argument to ``setsockopt() ``). The exact inventory depends on a board.
95
+ Socket options (an argument to `setsockopt() `). The exact
96
+ inventory depends on a MicroPython port.
95
97
96
98
Constants specific to WiPy:
97
99
98
- .. data :: socket. IPPROTO_SEC
100
+ .. data :: IPPROTO_SEC
99
101
100
102
Special protocol value to create SSL-compatible socket.
101
103
@@ -105,21 +107,22 @@ class socket
105
107
Methods
106
108
-------
107
109
108
- .. method :: socket.close
110
+ .. method :: socket.close()
109
111
110
- Mark the socket closed. Once that happens, all future operations on the socket
111
- object will fail. The remote end will receive no more data (after queued data is flushed).
112
+ Mark the socket closed and release all resources. Once that happens, all future operations
113
+ on the socket object will fail. The remote end will receive EOF indication if
114
+ supported by protocol.
112
115
113
116
Sockets are automatically closed when they are garbage-collected, but it is recommended
114
- to close() them explicitly, or to use a with statement around them.
117
+ to ` close() ` them explicitly as soon you finished working with them.
115
118
116
119
.. method :: socket.bind(address)
117
120
118
- Bind the socket to address. The socket must not already be bound.
121
+ Bind the socket to * address * . The socket must not already be bound.
119
122
120
123
.. method :: socket.listen([backlog])
121
124
122
- Enable a server to accept connections. If backlog is specified, it must be at least 0
125
+ Enable a server to accept connections. If * backlog * is specified, it must be at least 0
123
126
(if it's lower, it will be set to 0); and specifies the number of unaccepted connections
124
127
that the system will allow before refusing new connections. If not specified, a default
125
128
reasonable value is chosen.
@@ -133,7 +136,7 @@ Methods
133
136
134
137
.. method :: socket.connect(address)
135
138
136
- Connect to a remote socket at address.
139
+ Connect to a remote socket at * address * .
137
140
138
141
.. method :: socket.send(bytes)
139
142
@@ -144,11 +147,11 @@ Methods
144
147
.. method :: socket.sendall(bytes)
145
148
146
149
Send all data to the socket. The socket must be connected to a remote socket.
147
- Unlike `` send() ` `, this method will try to send all of data, by sending data
150
+ Unlike `send() `, this method will try to send all of data, by sending data
148
151
chunk by chunk consecutively.
149
152
150
153
The behavior of this method on non-blocking sockets is undefined. Due to this,
151
- on MicroPython, it's recommended to use `` write() ` ` method instead, which
154
+ on MicroPython, it's recommended to use `write() ` method instead, which
152
155
has the same "no short writes" policy for blocking sockets, and will return
153
156
number of bytes sent on non-blocking sockets.
154
157
@@ -160,33 +163,33 @@ Methods
160
163
.. method :: socket.sendto(bytes, address)
161
164
162
165
Send data to the socket. The socket should not be connected to a remote socket, since the
163
- destination socket is specified by ` address ` .
166
+ destination socket is specified by * address * .
164
167
165
168
.. method :: socket.recvfrom(bufsize)
166
169
167
- Receive data from the socket. The return value is a pair (bytes, address) where bytes is a
168
- bytes object representing the data received and address is the address of the socket sending
170
+ Receive data from the socket. The return value is a pair * (bytes, address) * where * bytes * is a
171
+ bytes object representing the data received and * address * is the address of the socket sending
169
172
the data.
170
173
171
174
.. method :: socket.setsockopt(level, optname, value)
172
175
173
176
Set the value of the given socket option. The needed symbolic constants are defined in the
174
- socket module (SO_* etc.). The value can be an integer or a bytes-like object representing
177
+ socket module (SO_* etc.). The * value * can be an integer or a bytes-like object representing
175
178
a buffer.
176
179
177
180
.. method :: socket.settimeout(value)
178
181
179
182
Set a timeout on blocking socket operations. The value argument can be a nonnegative floating
180
183
point number expressing seconds, or None. If a non-zero value is given, subsequent socket operations
181
- will raise an `` OSError ` ` exception if the timeout period value has elapsed before the operation has
184
+ will raise an `OSError ` exception if the timeout period value has elapsed before the operation has
182
185
completed. If zero is given, the socket is put in non-blocking mode. If None is given, the socket
183
186
is put in blocking mode.
184
187
185
188
.. admonition :: Difference to CPython
186
189
:class: attention
187
190
188
191
CPython raises a ``socket.timeout `` exception in case of timeout,
189
- which is an `` OSError ` ` subclass. MicroPython raises an OSError directly
192
+ which is an `OSError ` subclass. MicroPython raises an OSError directly
190
193
instead. If you use ``except OSError: `` to catch the exception,
191
194
your code will work both in MicroPython and CPython.
192
195
@@ -195,7 +198,7 @@ Methods
195
198
Set blocking or non-blocking mode of the socket: if flag is false, the socket is set to non-blocking,
196
199
else to blocking mode.
197
200
198
- This method is a shorthand for certain `` settimeout() ` ` calls:
201
+ This method is a shorthand for certain `settimeout() ` calls:
199
202
200
203
* ``sock.setblocking(True) `` is equivalent to ``sock.settimeout(None) ``
201
204
* ``sock.setblocking(False) `` is equivalent to ``sock.settimeout(0) ``
@@ -204,12 +207,12 @@ Methods
204
207
205
208
Return a file object associated with the socket. The exact returned type depends on the arguments
206
209
given to makefile(). The support is limited to binary modes only ('rb', 'wb', and 'rwb').
207
- CPython's arguments: `` encoding ``, `` errors `` and `` newline `` are not supported.
210
+ CPython's arguments: * encoding *, * errors * and * newline * are not supported.
208
211
209
212
.. admonition :: Difference to CPython
210
213
:class: attention
211
214
212
- As MicroPython doesn't support buffered streams, values of `` buffering ``
215
+ As MicroPython doesn't support buffered streams, values of * buffering *
213
216
parameter is ignored and treated as if it was 0 (unbuffered).
214
217
215
218
.. admonition :: Difference to CPython
@@ -220,19 +223,19 @@ Methods
220
223
221
224
.. method :: socket.read([size])
222
225
223
- Read up to size bytes from the socket. Return a bytes object. If `` size `` is not given, it
224
- reads all data available from the socket until `` EOF `` ; as such the method will not return until
226
+ Read up to size bytes from the socket. Return a bytes object. If * size * is not given, it
227
+ reads all data available from the socket until EOF; as such the method will not return until
225
228
the socket is closed. This function tries to read as much data as
226
229
requested (no "short reads"). This may be not possible with
227
230
non-blocking socket though, and then less data will be returned.
228
231
229
232
.. method :: socket.readinto(buf[, nbytes])
230
233
231
- Read bytes into the `` buf `` . If `` nbytes `` is specified then read at most
232
- that many bytes. Otherwise, read at most `` len(buf) `` bytes. Just as
233
- `` read() ` `, this method follows "no short reads" policy.
234
+ Read bytes into the * buf * . If * nbytes * is specified then read at most
235
+ that many bytes. Otherwise, read at most * len(buf) * bytes. Just as
236
+ `read() `, this method follows "no short reads" policy.
234
237
235
- Return value: number of bytes read and stored into `` buf `` .
238
+ Return value: number of bytes read and stored into * buf * .
236
239
237
240
.. method :: socket.readline()
238
241
@@ -245,6 +248,6 @@ Methods
245
248
Write the buffer of bytes to the socket. This function will try to
246
249
write all data to a socket (no "short writes"). This may be not possible
247
250
with a non-blocking socket though, and returned value will be less than
248
- the length of `` buf `` .
251
+ the length of * buf * .
249
252
250
253
Return value: number of bytes written.
0 commit comments