@@ -15,16 +15,6 @@ platforms.
1515 Some behavior may be platform dependent, since calls are made to the operating
1616 system socket APIs.
1717
18- For an introduction to socket programming (in C), see the following papers: An
19- Introductory 4.3BSD Interprocess Communication Tutorial, by Stuart Sechrest and
20- An Advanced 4.3BSD Interprocess Communication Tutorial, by Samuel J. Leffler et
21- al, both in the UNIX Programmer's Manual, Supplementary Documents 1 (sections
22- PS1:7 and PS1:8). The platform-specific reference material for the various
23- socket-related system calls are also a valuable source of information on the
24- details of socket semantics. For Unix, refer to the manual pages; for Windows,
25- see the WinSock (or Winsock 2) specification. For IPv6-ready APIs, readers may
26- want to refer to :rfc: `3493 ` titled Basic Socket Interface Extensions for IPv6.
27-
2818.. index :: object: socket
2919
3020The Python interface is a straightforward transliteration of the Unix system
@@ -35,68 +25,83 @@ in the C interface: as with :meth:`read` and :meth:`write` operations on Python
3525files, buffer allocation on receive operations is automatic, and buffer length
3626is implicit on send operations.
3727
38- Socket addresses are represented as follows: A single string is used for the
39- :const: `AF_UNIX ` address family. A pair ``(host, port) `` is used for the
40- :const: `AF_INET ` address family, where *host * is a string representing either a
41- hostname in Internet domain notation like ``'daring.cwi.nl' `` or an IPv4 address
42- like ``'100.50.200.5' ``, and *port * is an integral port number. For
43- :const: `AF_INET6 ` address family, a four-tuple ``(host, port, flowinfo,
44- scopeid) `` is used, where *flowinfo * and *scopeid * represents ``sin6_flowinfo ``
45- and ``sin6_scope_id `` member 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. Other address families are currently not
49- supported. The address format required by a particular socket object is
50- automatically selected based on the address family specified when the socket
51- object was created.
5228
53- For IPv4 addresses, two special forms are accepted instead of a host address:
54- the empty string represents :const: `INADDR_ANY `, and the string
55- ``'<broadcast>' `` represents :const: `INADDR_BROADCAST `. The behavior is not
56- available for IPv6 for backward compatibility, therefore, you may want to avoid
57- these if you intend to support IPv6 with your Python programs.
29+ Socket families
30+ ---------------
5831
59- If you use a hostname in the *host * portion of IPv4/v6 socket address, the
60- program may show a nondeterministic behavior, as Python uses the first address
61- returned from the DNS resolution. The socket address will be resolved
62- differently into an actual IPv4/v6 address, depending on the results from DNS
63- resolution and/or the host configuration. For deterministic behavior use a
64- numeric address in *host * portion.
32+ Depending on the system and the build options, various socket families
33+ are supported by this module.
6534
66- AF_NETLINK sockets are represented as pairs `` pid, groups ``.
35+ Socket addresses are represented as follows:
6736
37+ - A single string is used for the :const: `AF_UNIX ` address family.
6838
69- Linux-only support for TIPC is also available using the :const: `AF_TIPC `
70- address family. TIPC is an open, non-IP based networked protocol designed
71- for use in clustered computer environments. Addresses are represented by a
72- tuple, and the fields depend on the address type. The general tuple form is
73- ``(addr_type, v1, v2, v3 [, scope]) ``, where:
39+ - A pair ``(host, port) `` is used for the :const: `AF_INET ` address family,
40+ where *host * is a string representing either a hostname in Internet domain
41+ notation like ``'daring.cwi.nl' `` or an IPv4 address like ``'100.50.200.5' ``,
42+ and *port * is an integral port number.
7443
75- - * addr_type * is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or
76- TIPC_ADDR_ID.
77- - * scope * is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and
78- TIPC_NODE_SCOPE.
79- - If * addr_type * is TIPC_ADDR_NAME, then * v1 * is the server type, * v2 * is
80- the port identifier, and * v3 * should be 0 .
44+ - For :const: ` AF_INET6 ` address family, a four-tuple `` (host, port, flowinfo,
45+ scopeid) `` is used, where * flowinfo * and * scopeid * represent the `` sin6_flowinfo ``
46+ and `` sin6_scope_id `` members in :const: ` struct sockaddr_in6 ` in C. For
47+ :mod: ` socket ` module methods, * flowinfo * and * scopeid * can be omitted just for
48+ backward compatibility. Note, however, omission of * scopeid * can cause problems
49+ in manipulating scoped IPv6 addresses .
8150
82- If *addr_type * is TIPC_ADDR_NAMESEQ, then *v1 * is the server type, *v2 *
83- is the lower port number, and *v3 * is the upper port number.
51+ - :const: `AF_NETLINK ` sockets are represented as pairs ``(pid, groups) ``.
8452
85- If *addr_type * is TIPC_ADDR_ID, then *v1 * is the node, *v2 * is the
86- reference, and *v3 * should be set to 0.
53+ - Linux-only support for TIPC is available using the :const: `AF_TIPC `
54+ address family. TIPC is an open, non-IP based networked protocol designed
55+ for use in clustered computer environments. Addresses are represented by a
56+ tuple, and the fields depend on the address type. The general tuple form is
57+ ``(addr_type, v1, v2, v3 [, scope]) ``, where:
8758
88- If *addr_type * is TIPC_ADDR_ID, then *v1 * is the node, *v2 * is the
89- reference, and *v3 * should be set to 0.
59+ - *addr_type * is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or
60+ TIPC_ADDR_ID.
61+ - *scope * is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and
62+ TIPC_NODE_SCOPE.
63+ - If *addr_type * is TIPC_ADDR_NAME, then *v1 * is the server type, *v2 * is
64+ the port identifier, and *v3 * should be 0.
9065
66+ If *addr_type * is TIPC_ADDR_NAMESEQ, then *v1 * is the server type, *v2 *
67+ is the lower port number, and *v3 * is the upper port number.
68+
69+ If *addr_type * is TIPC_ADDR_ID, then *v1 * is the node, *v2 * is the
70+ reference, and *v3 * should be set to 0.
71+
72+ If *addr_type * is TIPC_ADDR_ID, then *v1 * is the node, *v2 * is the
73+ reference, and *v3 * should be set to 0.
74+
75+ - Certain other address families (:const: `AF_BLUETOOTH `, :const: `AF_PACKET `)
76+ support specific representations.
77+
78+ .. XXX document them!
79+
80+ For IPv4 addresses, two special forms are accepted instead of a host address:
81+ the empty string represents :const: `INADDR_ANY `, and the string
82+ ``'<broadcast>' `` represents :const: `INADDR_BROADCAST `. This behavior is not
83+ compatible with IPv6, therefore, you may want to avoid these if you intend
84+ to support IPv6 with your Python programs.
85+
86+ If you use a hostname in the *host * portion of IPv4/v6 socket address, the
87+ program may show a nondeterministic behavior, as Python uses the first address
88+ returned from the DNS resolution. The socket address will be resolved
89+ differently into an actual IPv4/v6 address, depending on the results from DNS
90+ resolution and/or the host configuration. For deterministic behavior use a
91+ numeric address in *host * portion.
9192
9293All errors raise exceptions. The normal exceptions for invalid argument types
9394and out-of-memory conditions can be raised; errors related to socket or address
94- semantics raise the error :exc: `socket.error `.
95+ semantics raise :exc: `socket.error ` or one of its subclasses .
9596
9697Non-blocking mode is supported through :meth: `~socket.setblocking `. A
9798generalization of this based on timeouts is supported through
9899:meth: `~socket.settimeout `.
99100
101+
102+ Module contents
103+ ---------------
104+
100105The module :mod: `socket ` exports the following constants and functions:
101106
102107
@@ -145,7 +150,8 @@ The module :mod:`socket` exports the following constants and functions:
145150
146151 These constants represent the address (and protocol) families, used for the
147152 first argument to :func: `socket `. If the :const: `AF_UNIX ` constant is not
148- defined then this protocol is unsupported.
153+ defined then this protocol is unsupported. More constants may be available
154+ depending on the system.
149155
150156
151157.. data :: SOCK_STREAM
@@ -155,8 +161,9 @@ The module :mod:`socket` exports the following constants and functions:
155161 SOCK_SEQPACKET
156162
157163 These constants represent the socket types, used for the second argument to
158- :func: `socket `. (Only :const: `SOCK_STREAM ` and :const: `SOCK_DGRAM ` appear to be
159- generally useful.)
164+ :func: `socket `. More constants may be available depending on the system.
165+ (Only :const: `SOCK_STREAM ` and :const: `SOCK_DGRAM ` appear to be generally
166+ useful.)
160167
161168
162169.. data :: SO_*
@@ -911,3 +918,21 @@ the interface::
911918
912919 # disabled promiscuous mode
913920 s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
921+
922+
923+ .. seealso ::
924+
925+ For an introduction to socket programming (in C), see the following papers:
926+
927+ - *An Introductory 4.3BSD Interprocess Communication Tutorial *, by Stuart Sechrest
928+
929+ - *An Advanced 4.3BSD Interprocess Communication Tutorial *, by Samuel J. Leffler et
930+ al,
931+
932+ both in the UNIX Programmer's Manual, Supplementary Documents 1 (sections
933+ PS1:7 and PS1:8). The platform-specific reference material for the various
934+ socket-related system calls are also a valuable source of information on the
935+ details of socket semantics. For Unix, refer to the manual pages; for Windows,
936+ see the WinSock (or Winsock 2) specification. For IPv6-ready APIs, readers may
937+ want to refer to :rfc: `3493 ` titled Basic Socket Interface Extensions for IPv6.
938+
0 commit comments