Commit 7bdfe775 authored by Antoine Pitrou's avatar Antoine Pitrou

Improve readability of the socket docs

parent 64a467d7
...@@ -14,16 +14,6 @@ platforms. ...@@ -14,16 +14,6 @@ platforms.
Some behavior may be platform dependent, since calls are made to the operating Some behavior may be platform dependent, since calls are made to the operating
system socket APIs. system socket APIs.
For an introduction to socket programming (in C), see the following papers: An
Introductory 4.3BSD Interprocess Communication Tutorial, by Stuart Sechrest and
An Advanced 4.3BSD Interprocess Communication Tutorial, by Samuel J. Leffler et
al, both in the UNIX Programmer's Manual, Supplementary Documents 1 (sections
PS1:7 and PS1:8). The platform-specific reference material for the various
socket-related system calls are also a valuable source of information on the
details of socket semantics. For Unix, refer to the manual pages; for Windows,
see the WinSock (or Winsock 2) specification. For IPv6-ready APIs, readers may
want to refer to :rfc:`3493` titled Basic Socket Interface Extensions for IPv6.
.. index:: object: socket .. index:: object: socket
The Python interface is a straightforward transliteration of the Unix system The 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 ...@@ -34,68 +24,83 @@ in the C interface: as with :meth:`read` and :meth:`write` operations on Python
files, buffer allocation on receive operations is automatic, and buffer length files, buffer allocation on receive operations is automatic, and buffer length
is implicit on send operations. is implicit on send operations.
Socket addresses are represented as follows: A single string is used for the
:const:`AF_UNIX` address family. A pair ``(host, port)`` is used for the
:const:`AF_INET` address family, where *host* is a string representing either a
hostname in Internet domain notation like ``'daring.cwi.nl'`` or an IPv4 address
like ``'100.50.200.5'``, and *port* is an integral port number. For
:const:`AF_INET6` address family, a four-tuple ``(host, port, flowinfo,
scopeid)`` is used, where *flowinfo* and *scopeid* represents ``sin6_flowinfo``
and ``sin6_scope_id`` member in :const:`struct sockaddr_in6` in C. For
:mod:`socket` module methods, *flowinfo* and *scopeid* can be omitted just for
backward compatibility. Note, however, omission of *scopeid* can cause problems
in manipulating scoped IPv6 addresses. Other address families are currently not
supported. The address format required by a particular socket object is
automatically selected based on the address family specified when the socket
object was created.
For IPv4 addresses, two special forms are accepted instead of a host address: Socket families
the empty string represents :const:`INADDR_ANY`, and the string ---------------
``'<broadcast>'`` represents :const:`INADDR_BROADCAST`. The behavior is not
available for IPv6 for backward compatibility, therefore, you may want to avoid
these if you intend to support IPv6 with your Python programs.
If you use a hostname in the *host* portion of IPv4/v6 socket address, the Depending on the system and the build options, various socket families
program may show a nondeterministic behavior, as Python uses the first address are supported by this module.
returned from the DNS resolution. The socket address will be resolved
differently into an actual IPv4/v6 address, depending on the results from DNS
resolution and/or the host configuration. For deterministic behavior use a
numeric address in *host* portion.
AF_NETLINK sockets are represented as pairs ``pid, groups``. Socket addresses are represented as follows:
- A single string is used for the :const:`AF_UNIX` address family.
Linux-only support for TIPC is also available using the :const:`AF_TIPC` - A pair ``(host, port)`` is used for the :const:`AF_INET` address family,
address family. TIPC is an open, non-IP based networked protocol designed where *host* is a string representing either a hostname in Internet domain
for use in clustered computer environments. Addresses are represented by a notation like ``'daring.cwi.nl'`` or an IPv4 address like ``'100.50.200.5'``,
tuple, and the fields depend on the address type. The general tuple form is and *port* is an integral port number.
``(addr_type, v1, v2, v3 [, scope])``, where:
- *addr_type* is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or - For :const:`AF_INET6` address family, a four-tuple ``(host, port, flowinfo,
TIPC_ADDR_ID. scopeid)`` is used, where *flowinfo* and *scopeid* represent the ``sin6_flowinfo``
- *scope* is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and and ``sin6_scope_id`` members in :const:`struct sockaddr_in6` in C. For
TIPC_NODE_SCOPE. :mod:`socket` module methods, *flowinfo* and *scopeid* can be omitted just for
- If *addr_type* is TIPC_ADDR_NAME, then *v1* is the server type, *v2* is backward compatibility. Note, however, omission of *scopeid* can cause problems
the port identifier, and *v3* should be 0. in manipulating scoped IPv6 addresses.
If *addr_type* is TIPC_ADDR_NAMESEQ, then *v1* is the server type, *v2* - :const:`AF_NETLINK` sockets are represented as pairs ``(pid, groups)``.
is the lower port number, and *v3* is the upper port number.
If *addr_type* is TIPC_ADDR_ID, then *v1* is the node, *v2* is the - Linux-only support for TIPC is available using the :const:`AF_TIPC`
reference, and *v3* should be set to 0. address family. TIPC is an open, non-IP based networked protocol designed
for use in clustered computer environments. Addresses are represented by a
tuple, and the fields depend on the address type. The general tuple form is
``(addr_type, v1, v2, v3 [, scope])``, where:
If *addr_type* is TIPC_ADDR_ID, then *v1* is the node, *v2* is the - *addr_type* is one of TIPC_ADDR_NAMESEQ, TIPC_ADDR_NAME, or
reference, and *v3* should be set to 0. TIPC_ADDR_ID.
- *scope* is one of TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, and
TIPC_NODE_SCOPE.
- If *addr_type* is TIPC_ADDR_NAME, then *v1* is the server type, *v2* is
the port identifier, and *v3* should be 0.
If *addr_type* is TIPC_ADDR_NAMESEQ, then *v1* is the server type, *v2*
is the lower port number, and *v3* is the upper port number.
If *addr_type* is TIPC_ADDR_ID, then *v1* is the node, *v2* is the
reference, and *v3* should be set to 0.
If *addr_type* is TIPC_ADDR_ID, then *v1* is the node, *v2* is the
reference, and *v3* should be set to 0.
- Certain other address families (:const:`AF_BLUETOOTH`, :const:`AF_PACKET`)
support specific representations.
.. XXX document them!
For IPv4 addresses, two special forms are accepted instead of a host address:
the empty string represents :const:`INADDR_ANY`, and the string
``'<broadcast>'`` represents :const:`INADDR_BROADCAST`. This behavior is not
compatible with IPv6, therefore, you may want to avoid these if you intend
to support IPv6 with your Python programs.
If you use a hostname in the *host* portion of IPv4/v6 socket address, the
program may show a nondeterministic behavior, as Python uses the first address
returned from the DNS resolution. The socket address will be resolved
differently into an actual IPv4/v6 address, depending on the results from DNS
resolution and/or the host configuration. For deterministic behavior use a
numeric address in *host* portion.
All errors raise exceptions. The normal exceptions for invalid argument types All errors raise exceptions. The normal exceptions for invalid argument types
and out-of-memory conditions can be raised; errors related to socket or address and out-of-memory conditions can be raised; errors related to socket or address
semantics raise the error :exc:`socket.error`. semantics raise :exc:`socket.error` or one of its subclasses.
Non-blocking mode is supported through :meth:`~socket.setblocking`. A Non-blocking mode is supported through :meth:`~socket.setblocking`. A
generalization of this based on timeouts is supported through generalization of this based on timeouts is supported through
:meth:`~socket.settimeout`. :meth:`~socket.settimeout`.
Module contents
---------------
The module :mod:`socket` exports the following constants and functions: The module :mod:`socket` exports the following constants and functions:
...@@ -144,7 +149,8 @@ The module :mod:`socket` exports the following constants and functions: ...@@ -144,7 +149,8 @@ The module :mod:`socket` exports the following constants and functions:
These constants represent the address (and protocol) families, used for the These constants represent the address (and protocol) families, used for the
first argument to :func:`socket`. If the :const:`AF_UNIX` constant is not first argument to :func:`socket`. If the :const:`AF_UNIX` constant is not
defined then this protocol is unsupported. defined then this protocol is unsupported. More constants may be available
depending on the system.
.. data:: SOCK_STREAM .. data:: SOCK_STREAM
...@@ -154,8 +160,9 @@ The module :mod:`socket` exports the following constants and functions: ...@@ -154,8 +160,9 @@ The module :mod:`socket` exports the following constants and functions:
SOCK_SEQPACKET SOCK_SEQPACKET
These constants represent the socket types, used for the second argument to These constants represent the socket types, used for the second argument to
:func:`socket`. (Only :const:`SOCK_STREAM` and :const:`SOCK_DGRAM` appear to be :func:`socket`. More constants may be available depending on the system.
generally useful.) (Only :const:`SOCK_STREAM` and :const:`SOCK_DGRAM` appear to be generally
useful.)
.. data:: SOCK_CLOEXEC .. data:: SOCK_CLOEXEC
SOCK_NONBLOCK SOCK_NONBLOCK
...@@ -950,3 +957,21 @@ the interface:: ...@@ -950,3 +957,21 @@ the interface::
# disabled promiscuous mode # disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF) s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
.. seealso::
For an introduction to socket programming (in C), see the following papers:
- *An Introductory 4.3BSD Interprocess Communication Tutorial*, by Stuart Sechrest
- *An Advanced 4.3BSD Interprocess Communication Tutorial*, by Samuel J. Leffler et
al,
both in the UNIX Programmer's Manual, Supplementary Documents 1 (sections
PS1:7 and PS1:8). The platform-specific reference material for the various
socket-related system calls are also a valuable source of information on the
details of socket semantics. For Unix, refer to the manual pages; for Windows,
see the WinSock (or Winsock 2) specification. For IPv6-ready APIs, readers may
want to refer to :rfc:`3493` titled Basic Socket Interface Extensions for IPv6.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment