Python Glossary Русский

Python 3.4+socketterm

socket.SocketKind

constants

An IntEnum of socket types: SOCK_STREAM (TCP), SOCK_DGRAM (UDP), SOCK_RAW and others.

Syntax

socket.SocketKind.SOCK_STREAM  # то же, что socket.SOCK_STREAM

sock.type returns a member of this enum. On Linux you can OR the flags SOCK_NONBLOCK and SOCK_CLOEXEC into the type, but since 3.7 sock.type hides them, so sock.type == SOCK_STREAM still holds and non-blocking mode is checked with getblocking() or gettimeout() == 0. As with AddressFamily, print() shows the number since 3.11; use .name for the name.

Examples

import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    print(s.type.name)  # → SOCK_DGRAM
    print(s.type is socket.SOCK_DGRAM)  # → True
import socket
if hasattr(socket, 'SOCK_NONBLOCK'):  # флаг есть только в Linux
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM | socket.SOCK_NONBLOCK)
    print(s.type == socket.SOCK_STREAM)  # → ? True — флаг в type не виден
    print(s.getblocking())  # → ? False
    s.close()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy