Skip to content

Commit d1cd533

Browse files
committed
docs/usocket: Elaborate descriptions.
Use the "usocket" module name everywhere. Use "MicroPython port" terminology. Suggest to avoid using IPPROTO_* constants in socket() call.
1 parent f36975b commit d1cd533

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

docs/library/usocket.rst

+19-6
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@ Functions
6868

6969
.. function:: socket(af=AF_INET, type=SOCK_STREAM, proto=IPPROTO_TCP)
7070

71-
Create a new socket using the given address family, socket type and protocol number.
71+
Create a new socket using the given address family, socket type and
72+
protocol number. Note that specifying *proto* in most cases is not
73+
required (and not recommended, as some MicroPython ports may omit
74+
``IPPROTO_*`` constants). Instead, *type* argument will select needed
75+
protocol automatically::
76+
77+
# Create STREAM TCP socket
78+
socket(AF_INET, SOCK_STREAM)
79+
# Create DGRAM UDP socket
80+
socket(AF_INET, SOCK_DGRAM)
7281

7382
.. function:: getaddrinfo(host, port)
7483

@@ -80,8 +89,8 @@ Functions
8089

8190
The following example shows how to connect to a given url::
8291

83-
s = socket.socket()
84-
s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][-1])
92+
s = usocket.socket()
93+
s.connect(usocket.getaddrinfo('www.micropython.org', 80)[0][-1])
8594

8695
.. admonition:: Difference to CPython
8796
:class: attention
@@ -102,7 +111,7 @@ Constants
102111
.. data:: AF_INET
103112
AF_INET6
104113

105-
Address family types. Availability depends on a particular board.
114+
Address family types. Availability depends on a particular `MicroPython port`.
106115

107116
.. data:: SOCK_STREAM
108117
SOCK_DGRAM
@@ -112,7 +121,11 @@ Constants
112121
.. data:: IPPROTO_UDP
113122
IPPROTO_TCP
114123

115-
IP protocol numbers.
124+
IP protocol numbers. Availability depends on a particular `MicroPython port`.
125+
Note that you don't need to specify these in a call to `usocket.socket()`,
126+
because `SOCK_STREAM` socket type automatically selects `IPPROTO_TCP`, and
127+
`SOCK_DGRAM` - `IPPROTO_UDP`. Thus, the only real use of these constants
128+
is as an argument to `setsockopt()`.
116129

117130
.. data:: usocket.SOL_*
118131

@@ -281,7 +294,7 @@ Methods
281294

282295
Return value: number of bytes written.
283296

284-
.. exception:: socket.error
297+
.. exception:: usocket.error
285298

286299
MicroPython does NOT have this exception.
287300

0 commit comments

Comments
 (0)