@@ -21,11 +21,47 @@ This module provides access to the BSD socket interface.
21
21
Socket address format(s)
22
22
------------------------
23
23
24
- The functions below which expect a network address, accept it in the format of
25
- *(ipv4_address, port) *, where *ipv4_address * is a string with dot-notation numeric
26
- IPv4 address, e.g. ``"8.8.8.8" ``, and port is integer port number in the range
27
- 1-65535. Note the domain names are not accepted as *ipv4_address *, they should be
28
- resolved first using `usocket.getaddrinfo() `.
24
+ The native socket address format of the ``usocket `` module is an opaque data type
25
+ returned by `getaddrinfo ` function, which must be used to resolve textual address
26
+ (including numeric addresses)::
27
+
28
+ sockaddr = usocket.getaddrinfo('www.micropython.org', 80)[0][-1]
29
+ # You must use getaddrinfo() even for numeric addresses
30
+ sockaddr = usocket.getaddrinfo('127.0.0.1', 80)[0][-1]
31
+ # Now you can use that address
32
+ sock.connect(addr)
33
+
34
+ Using `getaddrinfo ` is the most efficient (both in terms of memory and processing
35
+ power) and portable way to work with addresses.
36
+
37
+ However, ``socket `` module (note the difference with native MicroPython
38
+ ``usocket `` module described here) provides CPython-compatible way to specify
39
+ addresses using tuples, as described below. Note that depending on a
40
+ `MicroPython port `, ``socket `` module can be builtin or need to be
41
+ installed from `micropython-lib ` (as in the case of `MicroPython Unix port `),
42
+ and some ports still accept only numeric addresses in the tuple format,
43
+ and require to use `getaddrinfo ` function to resolve domain names.
44
+
45
+ Summing up:
46
+
47
+ * Always use `getaddrinfo ` when writing portable applications.
48
+ * Tuple addresses described below can be used as a shortcut for
49
+ quick hacks and interactive use, if your port supports them.
50
+
51
+ Tuple address format for ``socket `` module:
52
+
53
+ * IPv4: *(ipv4_address, port) *, where *ipv4_address * is a string with
54
+ dot-notation numeric IPv4 address, e.g. ``"8.8.8.8" ``, and *port * is and
55
+ integer port number in the range 1-65535. Note the domain names are not
56
+ accepted as *ipv4_address *, they should be resolved first using
57
+ `usocket.getaddrinfo() `.
58
+ * IPv6: *(ipv6_address, port, flowinfo, scopeid) *, where *ipv6_address *
59
+ is a string with colon-notation numeric IPv6 address, e.g. ``"2001:db8::1" ``,
60
+ and *port * is an integer port number in the range 1-65535. *flowinfo *
61
+ must be 0. *scopeid * is the interface scope identifier for link-local
62
+ addresses. Note the domain names are not accepted as *ipv6_address *,
63
+ they should be resolved first using `usocket.getaddrinfo() `. Availability
64
+ of IPv6 support depends on a `MicroPython port `.
29
65
30
66
Functions
31
67
---------
0 commit comments