@@ -138,9 +138,12 @@ generalization of this based on timeouts is supported through
138138Module contents
139139---------------
140140
141- The module :mod: `socket ` exports the following constants and functions:
141+ The module :mod: `socket ` exports the following elements.
142142
143143
144+ Exceptions
145+ ^^^^^^^^^^
146+
144147.. exception :: error
145148
146149 A deprecated alias of :exc: `OSError `.
@@ -186,6 +189,10 @@ The module :mod:`socket` exports the following constants and functions:
186189 .. versionchanged :: 3.3
187190 This class was made a subclass of :exc: `OSError `.
188191
192+
193+ Constants
194+ ^^^^^^^^^
195+
189196.. data :: AF_UNIX
190197 AF_INET
191198 AF_INET6
@@ -290,6 +297,43 @@ The module :mod:`socket` exports the following constants and functions:
290297 this platform.
291298
292299
300+ Functions
301+ ^^^^^^^^^
302+
303+ Creating sockets
304+ ''''''''''''''''
305+
306+ The following functions all create :ref: `socket objects <socket-objects >`.
307+
308+
309+ .. function :: socket([family[, type[, proto]]])
310+
311+ Create a new socket using the given address family, socket type and protocol
312+ number. The address family should be :const: `AF_INET ` (the default),
313+ :const: `AF_INET6 `, :const: `AF_UNIX `, :const: `AF_CAN ` or :const: `AF_RDS `. The
314+ socket type should be :const: `SOCK_STREAM ` (the default),
315+ :const: `SOCK_DGRAM `, :const: `SOCK_RAW ` or perhaps one of the other ``SOCK_ ``
316+ constants. The protocol number is usually zero and may be omitted in that
317+ case or :const: `CAN_RAW ` in case the address family is :const: `AF_CAN `.
318+
319+ .. versionchanged :: 3.3
320+ The AF_CAN family was added.
321+ The AF_RDS family was added.
322+
323+
324+ .. function :: socketpair([family[, type[, proto]]])
325+
326+ Build a pair of connected socket objects using the given address family, socket
327+ type, and protocol number. Address family, socket type, and protocol number are
328+ as for the :func: `.socket ` function above. The default family is :const: `AF_UNIX `
329+ if defined on the platform; otherwise, the default is :const: `AF_INET `.
330+ Availability: Unix.
331+
332+ .. versionchanged :: 3.2
333+ The returned socket objects now support the whole socket API, rather
334+ than a subset.
335+
336+
293337.. function :: create_connection(address[, timeout[, source_address]])
294338
295339 Connect to a TCP service listening on the Internet *address * (a 2-tuple
@@ -316,6 +360,40 @@ The module :mod:`socket` exports the following constants and functions:
316360 support for the :keyword: `with ` statement was added.
317361
318362
363+ .. function :: fromfd(fd, family, type[, proto])
364+
365+ Duplicate the file descriptor *fd * (an integer as returned by a file object's
366+ :meth: `fileno ` method) and build a socket object from the result. Address
367+ family, socket type and protocol number are as for the :func: `.socket ` function
368+ above. The file descriptor should refer to a socket, but this is not checked ---
369+ subsequent operations on the object may fail if the file descriptor is invalid.
370+ This function is rarely needed, but can be used to get or set socket options on
371+ a socket passed to a program as standard input or output (such as a server
372+ started by the Unix inet daemon). The socket is assumed to be in blocking mode.
373+
374+
375+ .. function :: fromshare(data)
376+
377+ Instantiate a socket from data obtained from the :meth: `socket.share `
378+ method. The socket is assumed to be in blocking mode.
379+
380+ Availability: Windows.
381+
382+ .. versionadded :: 3.3
383+
384+
385+ .. data :: SocketType
386+
387+ This is a Python type object that represents the socket object type. It is the
388+ same as ``type(socket(...)) ``.
389+
390+
391+ Other functions
392+ '''''''''''''''
393+
394+ The :mod: `socket ` module also offers various network-related services:
395+
396+
319397.. function :: getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)
320398
321399 Translate the *host */*port * argument into a sequence of 5-tuples that contain
@@ -445,46 +523,6 @@ The module :mod:`socket` exports the following constants and functions:
445523 ``'udp' ``, otherwise any protocol will match.
446524
447525
448- .. function :: socket([family[, type[, proto]]])
449-
450- Create a new socket using the given address family, socket type and protocol
451- number. The address family should be :const: `AF_INET ` (the default),
452- :const: `AF_INET6 `, :const: `AF_UNIX `, :const: `AF_CAN ` or :const: `AF_RDS `. The
453- socket type should be :const: `SOCK_STREAM ` (the default),
454- :const: `SOCK_DGRAM `, :const: `SOCK_RAW ` or perhaps one of the other ``SOCK_ ``
455- constants. The protocol number is usually zero and may be omitted in that
456- case or :const: `CAN_RAW ` in case the address family is :const: `AF_CAN `.
457-
458- .. versionchanged :: 3.3
459- The AF_CAN family was added.
460- The AF_RDS family was added.
461-
462-
463- .. function :: socketpair([family[, type[, proto]]])
464-
465- Build a pair of connected socket objects using the given address family, socket
466- type, and protocol number. Address family, socket type, and protocol number are
467- as for the :func: `.socket ` function above. The default family is :const: `AF_UNIX `
468- if defined on the platform; otherwise, the default is :const: `AF_INET `.
469- Availability: Unix.
470-
471- .. versionchanged :: 3.2
472- The returned socket objects now support the whole socket API, rather
473- than a subset.
474-
475-
476- .. function :: fromfd(fd, family, type[, proto])
477-
478- Duplicate the file descriptor *fd * (an integer as returned by a file object's
479- :meth: `fileno ` method) and build a socket object from the result. Address
480- family, socket type and protocol number are as for the :func: `.socket ` function
481- above. The file descriptor should refer to a socket, but this is not checked ---
482- subsequent operations on the object may fail if the file descriptor is invalid.
483- This function is rarely needed, but can be used to get or set socket options on
484- a socket passed to a program as standard input or output (such as a server
485- started by the Unix inet daemon). The socket is assumed to be in blocking mode.
486-
487-
488526.. function :: ntohl(x)
489527
490528 Convert 32-bit positive integers from network to host byte order. On machines
@@ -680,22 +718,6 @@ The module :mod:`socket` exports the following constants and functions:
680718 .. versionadded :: 3.3
681719
682720
683- .. function :: fromshare(data)
684-
685- Instantiate a socket from data obtained from :meth: `~socket.share `.
686- The socket is assumed to be in blocking mode.
687-
688- Availability: Windows.
689-
690- .. versionadded :: 3.3
691-
692-
693- .. data :: SocketType
694-
695- This is a Python type object that represents the socket object type. It is the
696- same as ``type(socket(...)) ``.
697-
698-
699721.. _socket-objects :
700722
701723Socket Objects
@@ -1106,14 +1128,14 @@ to sockets.
11061128
11071129.. method :: socket.share(process_id)
11081130
1109- :platform: Windows
1131+ Duplicate a socket and prepare it for sharing with a target process. The
1132+ target process must be provided with *process_id *. The resulting bytes object
1133+ can then be passed to the target process using some form of interprocess
1134+ communication and the socket can be recreated there using :func: `fromshare `.
1135+ Once this method has been called, it is safe to close the socket since
1136+ the operating system has already duplicated it for the target process.
11101137
1111- Duplacet a socket and prepare it for sharing with a target process. The
1112- target process must be provided with *process_id *. The resulting bytes object
1113- can then be passed to the target process using some form of interprocess
1114- communication and the socket can be recreated there using :func: `fromshare `.
1115- Once this method has been called, it is safe to close the socket since
1116- the operating system has already duplicated it for the target process.
1138+ Availability: Windows.
11171139
11181140 .. versionadded :: 3.3
11191141
0 commit comments