Skip to content

Commit 73cc260

Browse files
committed
bpo-12887: Document the availability and uses of the SO_ constants.
bpo-14345: Document SOL_SOCKET
1 parent 2db6482 commit 73cc260

File tree

1 file changed

+277
-1
lines changed

1 file changed

+277
-1
lines changed

Doc/library/socket.rst

Lines changed: 277 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,283 @@ Constants
277277

278278
.. versionadded:: 3.2
279279

280+
Socket level options
281+
''''''''''''''''''''
282+
283+
These constants are passed to :meth:`socket.setsockopt` and :meth:`socket.getsockopt`
284+
with a level of :const:`SOL_SOCKET` to set and get socket settings. Their availability
285+
and behavior is dependent upon the underlying OS. Basic descriptions of each option are
286+
documented below but your OS's documentation will be far more reliable. The following
287+
table serves a basic compatibility guide.
288+
289+
Unless otherwise noted, these options are available on Linux versions >= 2.3,
290+
FreeBSD >= 2 and Windows >= 2000.
291+
292+
======================= =========== =========== ================
293+
Socket Option Linux FreeBSD Windows
294+
======================= =========== =========== ================
295+
**SO_DEBUG** ✓ ✓ ✓ [1]_
296+
**SO_ACCEPTCONN** ✓ ✓ ✓
297+
**SO_REUSEADDR** ✓ ✓ ✓
298+
**SO_EXCLUSIVEADDRUSE** ✓
299+
**SO_KEEPALIVE** ✓ ✓ ✓
300+
**SO_DONTROUTE** ✓ ✓ ✓ [1]_
301+
**SO_BROADCAST** ✓ ✓ ✓ >= Vista
302+
**SO_USELOOPBACK** ✓ ✓ >= Vista [1]_
303+
**SO_LINGER** ✓ ✓ ✓
304+
**SO_OOBINLINE** ✓ ✓ ✓
305+
**SO_REUSEPORT** ✓ >= 3.9 ✓
306+
**SO_SNDBUF** ✓ ✓ ✓
307+
**SO_RCVBUF** ✓ ✓ ✓
308+
**SO_SNDLOWAT** ✓ ✓ ✓ [1]_
309+
**SO_RCVLOWAT** ✓ ✓ ✓ [1]_
310+
**SO_SNDTIMEO** ✓ ✓ ✓
311+
**SO_RCVTIMEO** ✓ ✓ ✓
312+
**SO_ERROR** ✓ ✓ ✓
313+
**SO_TYPE** ✓ ✓ ✓
314+
**SO_SETFIB** ✓ >= 7.1
315+
**SO_PASSCRED** ✓
316+
**SO_PEERCRED** ✓
317+
**SO_PASSSEC** ✓ >= 2.6.18
318+
**SO_PEERSEC** ✓ >= 2.6.18
319+
**SO_BINDTODEVICE** ✓
320+
**SO_PRIORITY** ✓
321+
**SO_MARK** ✓ >= 2.6.25
322+
**SO_DOMAIN** ✓ >= 2.6.32
323+
**SO_PROTOCOL** ✓ >= 2.6.32 ✓ >= 8.3
324+
======================= =========== =========== ================
325+
326+
.. [1] On Windows these options exist purely for compatibility
327+
and are `not functional <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_.
328+
329+
.. data:: SOL_SOCKET
330+
331+
This is the level parameter passed to :meth:`socket.setsockopt`
332+
and :meth:`socket.getsockopt` to change socket level parameters.
333+
334+
For example, this is how to change a socket's sending buffer size. ::
335+
336+
>>> s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
337+
16384
338+
>>> s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)
339+
>>> s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
340+
2048
341+
342+
.. data:: SO_DEBUG
343+
344+
Setting this option to 1 on a socket causes it to print out debugging
345+
information into the kernel log. (Viewable with dmesg)
346+
347+
.. Availability: Linux, Windows `(non functional) <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_
348+
349+
.. data:: SO_ACCEPTCONN
350+
351+
Returns 1 if the socket is marked to accept connections with
352+
:meth:`socket.listen`. This option is read only.
353+
354+
.. Availability: Linux, Windows
355+
356+
.. data:: SO_REUSEADDR
357+
358+
Allows a socket to be able to bind to an address that was previously
359+
bound to. When a socket shuts down, whether by virtue of the program
360+
exiting or an explicit call to shut it down, it takes the OS some time
361+
to terminate existing connections and perform the proper shut down
362+
procedure. SO_REUSEADDR allows bypassing of this behavior and permits
363+
another socket to be able to bind to the address.
364+
365+
.. Availability: Linux, Windows
366+
367+
.. data:: SO_EXCLUSIVEADDRUSE
368+
369+
Allows overriding SO_REUSEADDR behavior for exclusive access to an
370+
address and port for high availability services.
371+
372+
.. seealso::
373+
374+
`Microsoft's documentation <https://msdn.microsoft.com/en-us/library/windows/desktop/cc150667(v=vs.85).aspx>`_
375+
on this option.
376+
377+
.. Availability: Windows
378+
379+
.. data:: SO_KEEPALIVE
380+
381+
Set to 1 to enable sending of keep-alive packets on certain types of
382+
sockets.
383+
384+
.. Availability: Linux, Windows
385+
386+
.. data:: SO_DONTROUTE
387+
388+
Indicates that packets sent through this socket should be routed
389+
through the interface its bound to.
390+
391+
.. Availability: Linux, Windows `(non functional) <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_
392+
393+
.. data:: SO_BROADCAST
394+
395+
This option may be set to 1 to enable broadcasting messages, if
396+
supported by the protocol. Note that IPV6 (:const:`AF_INET6`) does
397+
not support broadcasting.
398+
399+
.. Availability: Linux, Windows >= Vista.
400+
401+
.. data:: SO_USELOOPBACK
402+
403+
.. Availability: FreeBSD, Windows >= Vista `(non functional) <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_
404+
405+
.. data:: SO_LINGER
406+
407+
When a socket is set to linger. A call to :meth:`socket.close` or
408+
:meth:`socket.shutdown` will not return until all queued messages
409+
for the socket have been successfully sent or the linger timeout
410+
is reached. If the socket is closed due to the program exiting, it
411+
will linger in the background.
412+
413+
The value for this option is a struct on most operating systems.
414+
Consult your OS documentation for the struct's details.
415+
416+
.. Availability: Linux, Windows
417+
418+
.. data:: SO_OOBINLINE
419+
420+
Allows receiving of out of band data in the normal stream.
421+
422+
.. seealso::
423+
424+
`Out-of-band data <https://www.ibm.com/support/knowledgecenter/en/ssw_i5_54/rzab6/coobd.htm>`_
425+
for an in depth explanation.
426+
427+
.. Availability: Linux, Windows
428+
429+
.. data:: SO_REUSEPORT
430+
431+
.. Availability: Linux >= 3.9 (for TCP/UDP)
432+
433+
.. data:: SO_SNDBUF
434+
SO_RCVBUF
435+
436+
Sets the size of the sending and receiving buffers for this socket in
437+
bytes. Most operating systems impose an upper limit on the size of
438+
these buffers.
439+
440+
.. Availability: Linux, Windows
441+
442+
.. data::
443+
SO_RCVLOWAT
444+
SO_SNDLOWAT
445+
446+
SO_RCVLOWAT sets the minimum number of bytes that must be present in
447+
the socket's internal receive buffer before they are passed on to able
448+
read call. SO_SNDLOWAT similiary sets the minimum bytes before data is
449+
sent from the send buffer to the socket protocol.
450+
451+
SO_SNDLOWAT is read-only on Linux and SO_RCVLOWAT is read-only on
452+
Linux versions below 2.4.
453+
454+
Both these values default to 1.
455+
456+
.. Availability: Linux, Windows `(non functional) <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740532(v=vs.85).aspx>`_
457+
458+
.. data:: SO_RCVTIMEO
459+
SO_SNDTIMEO
460+
461+
Specifies the amount of time send and receive calls for this socket willl
462+
block before timing out. The default timeout of zero means that operations
463+
will never time out. On Linux this is a `struct timeval`, on Windows this
464+
is the time in milliseconds.
465+
466+
.. Availability: Linux, Windows
467+
468+
.. data:: SO_ERROR
469+
470+
Gets and resets the pending socket error.
471+
472+
.. Availability: Linux, Windows
473+
474+
.. data:: SO_TYPE
475+
476+
Gets the type of the socket. This type corresponds to the type as
477+
defined in the :func:`socket.socket` function. Examples include
478+
:const:`SOCK_STREAM` and :const:`SOCK_DGRAM`.
479+
480+
.. Availability: Linux, Windows
481+
482+
.. data:: SO_SETFIB
483+
484+
Sets the FIB (routing table) for the socket.
485+
486+
Availability: FreeBSD >= 7.1
487+
488+
.. versionadded:: 3.1
489+
490+
.. data:: SO_PASSCRED
491+
SO_PEERCRED
492+
493+
Allows for the passing of SCM credentials over unix sockets.
494+
See the end of :func:`socket.recvmsg` for details.
495+
496+
.. versionadded:: 3.3
497+
498+
.. Availability: Linux
499+
500+
.. data:: SO_PASSSEC
501+
SO_PEERSEC
502+
503+
Allows for the passing of security contexts over unix sockets.
504+
505+
.. versionadded:: 3.6
506+
507+
.. Availability: Linux >= 2.6.13
508+
509+
.. data:: SO_BINDTODEVICE
510+
511+
Binds a socket to a particular network interface device like "eth0",
512+
When bound, only packets received from that particular device are
513+
processsed by the socket.
514+
515+
.. versionadded:: 3.1
516+
517+
.. Availability: Linux
518+
519+
.. data:: SO_PRIORITY
520+
521+
Sets the protocol-defined priority for each packet sent on this socket.
522+
Packets with higher priority may be processed first depending on the
523+
queueing mechanism of the network interface.
524+
525+
.. versionadded:: 3.4
526+
527+
.. Availability: Linux
528+
529+
.. data:: SO_MARK
530+
531+
Sets the mark on each packet sent through this socket. This may be used
532+
for routing or packet filtering.
533+
534+
.. versionadded:: 3.5
535+
536+
.. Availability: Linux >= 2.6.25
537+
538+
.. data:: SO_DOMAIN
539+
SO_PROTOCOL
540+
541+
Passing ``SO_DOMAIN`` to :meth:`socket.getsockopt` allows for the retrival
542+
of the ``family`` value as defined in the :func:`socket.socket` function.
543+
``SO_PROTOCOL`` returns the ``proto`` value. Both these options are read only.
544+
545+
The value returned for the ``family`` is an integer and not one of the
546+
friendly constants above like :const:`AF_INET`. In order to get a constant
547+
value back you can use the AddressFamily enum. ::
548+
549+
>>> family = s.getsockopt(socket.SOL_SOCKET, socket.SO_DOMAIN)
550+
>>> socket.AddressFamily(family)
551+
<AddressFamily.AF_INET: 2>
552+
553+
Availability: Linux >= 2.6.32.
554+
555+
.. versionadded:: 3.6
556+
280557
.. data:: SO_*
281558
SOMAXCONN
282559
MSG_*
@@ -300,7 +577,6 @@ Constants
300577
provided.
301578

302579
.. versionchanged:: 3.6
303-
``SO_DOMAIN``, ``SO_PROTOCOL``, ``SO_PEERSEC``, ``SO_PASSSEC``,
304580
``TCP_USER_TIMEOUT``, ``TCP_CONGESTION`` were added.
305581

306582
.. versionchanged:: 3.7

0 commit comments

Comments
 (0)