@@ -14,16 +14,6 @@ platforms.
1414 Some behavior may be platform dependent, since calls are made to the operating
1515 system socket APIs.
1616
17- For an introduction to socket programming (in C), see the following papers: An
18- Introductory 4.3BSD Interprocess Communication Tutorial, by Stuart Sechrest and
19- An Advanced 4.3BSD Interprocess Communication Tutorial, by Samuel J. Leffler et
20- al, both in the UNIX Programmer's Manual, Supplementary Documents 1 (sections
21- PS1:7 and PS1:8). The platform-specific reference material for the various
22- socket-related system calls are also a valuable source of information on the
23- details of socket semantics. For Unix, refer to the manual pages; for Windows,
24- see the WinSock (or Winsock 2) specification. For IPv6-ready APIs, readers may
25- want to refer to :rfc: `3493 ` titled Basic Socket Interface Extensions for IPv6.
26-
2717.. index :: object: socket
2818
2919The Python interface is a straightforward transliteration of the Unix system
@@ -34,68 +24,83 @@ in the C interface: as with :meth:`read` and :meth:`write` operations on Python
3424files, buffer allocation on receive operations is automatic, and buffer length
3525is implicit on send operations.
3626
37- Socket addresses are represented as follows: A single string is used for the
38- :const: `AF_UNIX ` address family. A pair ``(host, port) `` is used for the
39- :const: `AF_INET ` address family, where *host * is a string representing either a
40- hostname in Internet domain notation like ``'daring.cwi.nl' `` or an IPv4 address
41- like ``'100.50.200.5' ``, and *port * is an integral port number. For
42- :const: `AF_INET6 ` address family, a four-tuple ``(host, port, flowinfo,
43- scopeid) `` is used, where *flowinfo * and *scopeid * represents ``sin6_flowinfo ``
44- and ``sin6_scope_id `` member in :const: `struct sockaddr_in6 ` in C. For
45- :mod: `socket ` module methods, *flowinfo * and *scopeid * can be omitted just for
46- backward compatibility. Note, however, omission of *scopeid * can cause problems
47- in manipulating scoped IPv6 addresses. Other address families are currently not
48- supported. The address format required by a particular socket object is
49- automatically selected based on the address family specified when the socket
50- object was created.
5127
52- For IPv4 addresses, two special forms are accepted instead of a host address:
53- the empty string represents :const: `INADDR_ANY `, and the string
54- ``'<broadcast>' `` represents :const: `INADDR_BROADCAST `. The behavior is not
55- available for IPv6 for backward compatibility, therefore, you may want to avoid
56- these if you intend to support IPv6 with your Python programs.
28+ Socket families
29+ ---------------
5730
58- If you use a hostname in the *host * portion of IPv4/v6 socket address, the
59- program may show a nondeterministic behavior, as Python uses the first address
60- returned from the DNS resolution. The socket address will be resolved
61- differently into an actual IPv4/v6 address, depending on the results from DNS
62- resolution and/or the host configuration. For deterministic behavior use a
63- numeric address in *host * portion.
31+ Depending on the system and the build options, various socket families
32+ are supported by this module.
6433
65- AF_NETLINK sockets are represented as pairs `` pid, groups ``.
34+ Socket addresses are represented as follows:
6635
36+ - A single string is used for the :const: `AF_UNIX ` address family.
6737
68- Linux-only support for TIPC is also available using the :const: `AF_TIPC `
69- address family. TIPC is an open, non-IP based networked protocol designed
70- for use in clustered computer environments. Addresses are represented by a
71- tuple, and the fields depend on the address type. The general tuple form is
72- ``(addr_type, v1, v2, v3 [, scope]) ``, where:
38+ - A pair ``(host, port) `` is used for the :const: `AF_INET ` address family,
39+ where *host * is a string representing either a hostname in Internet domain
40+ notation like ``'daring.cwi.nl' `` or an IPv4 address like ``'100.50.200.5' ``,
41+ and *port * is an integral port number.
7342
74- - * addr_type * is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or
75- TIPC_ADDR_ID.
76- - * scope * is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and
77- TIPC_NODE_SCOPE.
78- - If * addr_type * is TIPC_ADDR_NAME, then * v1 * is the server type, * v2 * is
79- the port identifier, and * v3 * should be 0 .
43+ - For :const: ` AF_INET6 ` address family, a four-tuple `` (host, port, flowinfo,
44+ scopeid) `` is used, where * flowinfo * and * scopeid * represent the `` sin6_flowinfo ``
45+ and `` sin6_scope_id `` members in :const: ` struct sockaddr_in6 ` in C. For
46+ :mod: ` socket ` module methods, * flowinfo * and * scopeid * can be omitted just for
47+ backward compatibility. Note, however, omission of * scopeid * can cause problems
48+ in manipulating scoped IPv6 addresses .
8049
81- If *addr_type * is TIPC_ADDR_NAMESEQ, then *v1 * is the server type, *v2 *
82- is the lower port number, and *v3 * is the upper port number.
50+ - :const: `AF_NETLINK ` sockets are represented as pairs ``(pid, groups) ``.
8351
84- If *addr_type * is TIPC_ADDR_ID, then *v1 * is the node, *v2 * is the
85- reference, and *v3 * should be set to 0.
52+ - Linux-only support for TIPC is available using the :const: `AF_TIPC `
53+ address family. TIPC is an open, non-IP based networked protocol designed
54+ for use in clustered computer environments. Addresses are represented by a
55+ tuple, and the fields depend on the address type. The general tuple form is
56+ ``(addr_type, v1, v2, v3 [, scope]) ``, where:
8657
87- If *addr_type * is TIPC_ADDR_ID, then *v1 * is the node, *v2 * is the
88- reference, and *v3 * should be set to 0.
58+ - *addr_type * is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or
59+ TIPC_ADDR_ID.
60+ - *scope * is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and
61+ TIPC_NODE_SCOPE.
62+ - If *addr_type * is TIPC_ADDR_NAME, then *v1 * is the server type, *v2 * is
63+ the port identifier, and *v3 * should be 0.
8964
65+ If *addr_type * is TIPC_ADDR_NAMESEQ, then *v1 * is the server type, *v2 *
66+ is the lower port number, and *v3 * is the upper port number.
67+
68+ If *addr_type * is TIPC_ADDR_ID, then *v1 * is the node, *v2 * is the
69+ reference, and *v3 * should be set to 0.
70+
71+ If *addr_type * is TIPC_ADDR_ID, then *v1 * is the node, *v2 * is the
72+ reference, and *v3 * should be set to 0.
73+
74+ - Certain other address families (:const: `AF_BLUETOOTH `, :const: `AF_PACKET `)
75+ support specific representations.
76+
77+ .. XXX document them!
78+
79+ For IPv4 addresses, two special forms are accepted instead of a host address:
80+ the empty string represents :const: `INADDR_ANY `, and the string
81+ ``'<broadcast>' `` represents :const: `INADDR_BROADCAST `. This behavior is not
82+ compatible with IPv6, therefore, you may want to avoid these if you intend
83+ to support IPv6 with your Python programs.
84+
85+ If you use a hostname in the *host * portion of IPv4/v6 socket address, the
86+ program may show a nondeterministic behavior, as Python uses the first address
87+ returned from the DNS resolution. The socket address will be resolved
88+ differently into an actual IPv4/v6 address, depending on the results from DNS
89+ resolution and/or the host configuration. For deterministic behavior use a
90+ numeric address in *host * portion.
9091
9192All errors raise exceptions. The normal exceptions for invalid argument types
9293and out-of-memory conditions can be raised; errors related to socket or address
93- semantics raise the error :exc: `socket.error `.
94+ semantics raise :exc: `socket.error ` or one of its subclasses .
9495
9596Non-blocking mode is supported through :meth: `~socket.setblocking `. A
9697generalization of this based on timeouts is supported through
9798:meth: `~socket.settimeout `.
9899
100+
101+ Module contents
102+ ---------------
103+
99104The module :mod: `socket ` exports the following constants and functions:
100105
101106
@@ -144,7 +149,8 @@ The module :mod:`socket` exports the following constants and functions:
144149
145150 These constants represent the address (and protocol) families, used for the
146151 first argument to :func: `socket `. If the :const: `AF_UNIX ` constant is not
147- defined then this protocol is unsupported.
152+ defined then this protocol is unsupported. More constants may be available
153+ depending on the system.
148154
149155
150156.. data :: SOCK_STREAM
@@ -154,8 +160,9 @@ The module :mod:`socket` exports the following constants and functions:
154160 SOCK_SEQPACKET
155161
156162 These constants represent the socket types, used for the second argument to
157- :func: `socket `. (Only :const: `SOCK_STREAM ` and :const: `SOCK_DGRAM ` appear to be
158- generally useful.)
163+ :func: `socket `. More constants may be available depending on the system.
164+ (Only :const: `SOCK_STREAM ` and :const: `SOCK_DGRAM ` appear to be generally
165+ useful.)
159166
160167.. data :: SOCK_CLOEXEC
161168 SOCK_NONBLOCK
@@ -950,3 +957,21 @@ the interface::
950957
951958 # disabled promiscuous mode
952959 s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
960+
961+
962+ .. seealso ::
963+
964+ For an introduction to socket programming (in C), see the following papers:
965+
966+ - *An Introductory 4.3BSD Interprocess Communication Tutorial *, by Stuart Sechrest
967+
968+ - *An Advanced 4.3BSD Interprocess Communication Tutorial *, by Samuel J. Leffler et
969+ al,
970+
971+ both in the UNIX Programmer's Manual, Supplementary Documents 1 (sections
972+ PS1:7 and PS1:8). The platform-specific reference material for the various
973+ socket-related system calls are also a valuable source of information on the
974+ details of socket semantics. For Unix, refer to the manual pages; for Windows,
975+ see the WinSock (or Winsock 2) specification. For IPv6-ready APIs, readers may
976+ want to refer to :rfc: `3493 ` titled Basic Socket Interface Extensions for IPv6.
977+
0 commit comments