PyDocs

Networking and Interprocess Communication

Networking and Interprocess Communication

socket

not WASI

This module provides socket operations and some related functions.
On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
On other systems, it only supports IP. Functions specific for a
socket are available as methods of the socket object.

Functions:

socket() -- create a new socket object
socketpair() -- create a pair of new socket objects [*]
fromfd() -- create a socket object from an open file descriptor [*]
send_fds() -- Send file descriptor to the socket.
recv_fds() -- Receive file descriptors from the socket.
fromshare() -- create a socket object from data received from socket.share() [*]
gethostname() -- return the current hostname
gethostbyname() -- map a hostname to its IP number
gethostbyaddr() -- map an IP number or hostname to DNS info
getservbyname() -- map a service name and a protocol name to a port number
getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
socket.getdefaulttimeout() -- get the default timeout value
socket.setdefaulttimeout() -- set the default timeout value
create_connection() -- connects to an address, with an optional timeout and
                       optional source address.
create_server() -- create a TCP socket and bind it to a specified address.

 [*] not available on all platforms!

Special objects:

SocketType -- type object for socket objects
error -- exception raised for I/O errors
has_ipv6 -- boolean value indicating if IPv6 is supported

IntEnum constants:

AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)

Integer constants:

Many other constants may be defined; these may be used in calls to
the setsockopt() and getsockopt() methods.
Symbols in socket

a

AF_ALG
Constants for Linux Kernel cryptography.
AF_BLUETOOTH
Integer constants for use with Bluetooth addresses.
AF_CAN
Many constants of these forms, documented in the Linux documentation, are also defined in the socket module.
AF_DIVERT
These two constants, documented in the FreeBSD divert(4) manual page, are also defined in the socket module.
AF_HYPERV
Constants for Windows Hyper-V sockets for host/guest communications.
AF_INET
These constants represent the address (and protocol) families, used for the first argument to socket().
AF_INET6
These constants represent the address (and protocol) families, used for the first argument to socket().
AF_LINK
Availability: BSD, macOS.
AF_PACKET
Many constants of these forms, documented in the Linux documentation, are also defined in the socket module.
AF_QIPCRTR
Constant for Qualcomm’s IPC router protocol, used to communicate with service providing remote processors.
AF_RDS
Many constants of these forms, documented in the Linux documentation, are also defined in the socket module.
AF_UNIX
These constants represent the address (and protocol) families, used for the first argument to socket().
AF_UNSPEC
AF_UNSPEC means that getaddrinfo() should return socket addresses for any address family (either IPv4, IPv6, or any other) that can be used.
AF_VSOCK
Constants for Linux host/guest communication.

b

BDADDR_ANY
These are string constants containing Bluetooth addresses with special meanings.
BDADDR_BREDR
These constants describe the Bluetooth address type when binding or connecting a BTPROTO_L2CAP socket.
BDADDR_LE_PUBLIC
These constants describe the Bluetooth address type when binding or connecting a BTPROTO_L2CAP socket.
BDADDR_LE_RANDOM
These constants describe the Bluetooth address type when binding or connecting a BTPROTO_L2CAP socket.
BDADDR_LOCAL
These are string constants containing Bluetooth addresses with special meanings.
BTPROTO_HCI
Integer constants for use with Bluetooth addresses.
BTPROTO_L2CAP
Integer constants for use with Bluetooth addresses.
BTPROTO_RFCOMM
Integer constants for use with Bluetooth addresses.
BTPROTO_SCO
Integer constants for use with Bluetooth addresses.

c

CAN_BCM
CAN_BCM, in the CAN protocol family, is the broadcast manager (BCM) protocol.
CAN_ISOTP
CAN_ISOTP, in the CAN protocol family, is the ISO-TP (ISO 15765-2) protocol.
CAN_J1939
CAN_J1939, in the CAN protocol family, is the SAE J1939 protocol.
CAN_RAW_FD_FRAMES
Enables CAN FD support in a CAN_RAW socket.
CAN_RAW_JOIN_FILTERS
Joins the applied CAN filters such that only CAN frames that match all given CAN filters are passed to user space.
close
Close an integer socket file descriptor.
CMSG_LEN
Return the total length, without trailing padding, of an ancillary data item with associated data of the given length.
CMSG_SPACE
Return the buffer size needed for recvmsg() to receive an ancillary data item with associated data of the given length, along with any trailing padding.
create_connection
Connect to *address* and return the socket object.
create_server
Convenience function which creates a SOCK_STREAM type socket bound to *address* (a 2-tuple (host, port)) and return the socket object.

e

error
Base class for I/O related errors.
ETH_P_ALL
ETH_P_ALL can be used in the socket constructor as proto for the AF_PACKET family in order to capture every packet, regardless of protocol.
ETHERTYPE_ARP
IEEE 802.3 protocol number.
ETHERTYPE_IP
IEEE 802.3 protocol number.
ETHERTYPE_IPV6
IEEE 802.3 protocol number.
ETHERTYPE_VLAN
IEEE 802.3 protocol number.

f

fromfd
Create a socket object from a duplicate of the given file descriptor.
fromshare
Instantiate a socket from data obtained from the socket.share() method.

g

gaierror
A subclass of OSError, this exception is raised for address-related errors by getaddrinfo() and getnameinfo().
getaddrinfo
Resolve host and port into list of address info entries.
getdefaulttimeout
Returns the default timeout in seconds (float) for new socket objects.
getfqdn
Get fully qualified domain name from name.
gethostbyaddr
Return the true host name, a list of aliases, and a list of IP addresses, for a host.
gethostbyname
Return the IP address (a string of the form '255.255.255.255') for a host.
gethostbyname_ex
Return the true host name, a list of aliases, and a list of IP addresses, for a host.
gethostname
Return the current host name.
getnameinfo
Get host and port for a sockaddr.
getprotobyname
Return the protocol number for the named protocol.
getservbyname
Return a port number from a service name and protocol name.
getservbyport
Return the service name from a port number and protocol name.

h

has_dualstack_ipv6
Return True if the platform supports creating a SOCK_STREAM socket which can handle both AF_INET and AF_INET6 (IPv4 / IPv6) connections.
has_ipv6
This constant contains a boolean value which indicates if IPv6 is supported on this platform.
HCI_CHANNEL_CONTROL
Possible values for channel field in the BTPROTO_HCI address.
HCI_CHANNEL_LOGGING
Possible values for channel field in the BTPROTO_HCI address.
HCI_CHANNEL_MONITOR
Possible values for channel field in the BTPROTO_HCI address.
HCI_CHANNEL_RAW
Possible values for channel field in the BTPROTO_HCI address.
HCI_CHANNEL_USER
Possible values for channel field in the BTPROTO_HCI address.
HCI_DATA_DIR
Option names for use with BTPROTO_HCI.
HCI_DEV_NONE
The device_id value used to create an HCI socket that isn’t specific to a single Bluetooth adapter.
HCI_FILTER
Option names for use with BTPROTO_HCI.
HCI_TIME_STAMP
Option names for use with BTPROTO_HCI.
herror
A subclass of OSError, this exception is raised for address-related errors, i.e.
htonl
Convert a 32-bit unsigned integer from host to network byte order.
htons
Convert a 16-bit unsigned integer from host to network byte order.
HV_GUID_BROADCAST
Constants for Windows Hyper-V sockets for host/guest communications.
HV_GUID_CHILDREN
Constants for Windows Hyper-V sockets for host/guest communications.
HV_GUID_LOOPBACK
Constants for Windows Hyper-V sockets for host/guest communications.
HV_GUID_PARENT
Constants for Windows Hyper-V sockets for host/guest communications.
HV_GUID_WILDCARD
Constants for Windows Hyper-V sockets for host/guest communications.
HV_GUID_ZERO
Constants for Windows Hyper-V sockets for host/guest communications.
HV_PROTOCOL_RAW
Constants for Windows Hyper-V sockets for host/guest communications.
HVSOCKET_ADDRESS_FLAG_PASSTHRU
Constants for Windows Hyper-V sockets for host/guest communications.
HVSOCKET_CONNECT_TIMEOUT
Constants for Windows Hyper-V sockets for host/guest communications.
HVSOCKET_CONNECT_TIMEOUT_MAX
Constants for Windows Hyper-V sockets for host/guest communications.
HVSOCKET_CONNECTED_SUSPEND
Constants for Windows Hyper-V sockets for host/guest communications.

i

if_indextoname
Returns the interface name corresponding to the interface index if_index.
if_nameindex
Returns a list of network interface information (index, name) tuples.
if_nametoindex
Returns the interface index corresponding to the interface name if_name.
inet_aton
Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions.
inet_ntoa
Convert an IP address from 32-bit packed binary format to string format.
inet_ntop
Convert a packed IP address of the given family to string format.
inet_pton
Convert an IP address from string format to a packed string suitable for use with low-level network functions.
IOCTL_VM_SOCKETS_GET_LOCAL_CID
Constants for Linux host/guest communication.

l

L2CAP_LM
Used in the option name and value argument to the setsockopt() and getsockopt() methods of Bluetooth socket objects.
LOCAL_CREDS
LOCAL_CREDS and LOCAL_CREDS_PERSISTENT can be used with SOCK_DGRAM, SOCK_STREAM sockets, equivalent to Linux/DragonFlyBSD SO_PASSCRED, while LOCAL_CREDS sends the credentials at first read, LOCAL_CRE…
LOCAL_CREDS_PERSISTENT
LOCAL_CREDS and LOCAL_CREDS_PERSISTENT can be used with SOCK_DGRAM, SOCK_STREAM sockets, equivalent to Linux/DragonFlyBSD SO_PASSCRED, while LOCAL_CREDS sends the credentials at first read, LOCAL_CRE…

n

ntohl
Convert a 32-bit unsigned integer from network to host byte order.
ntohs
Convert a 16-bit unsigned integer from network to host byte order.

p

PF_CAN
Many constants of these forms, documented in the Linux documentation, are also defined in the socket module.
PF_DIVERT
These two constants, documented in the FreeBSD divert(4) manual page, are also defined in the socket module.
PF_PACKET
Many constants of these forms, documented in the Linux documentation, are also defined in the socket module.
PF_RDS
Many constants of these forms, documented in the Linux documentation, are also defined in the socket module.

r

recv_fds
descriptors, msg_flags, address)

s

SCM_CREDS2
LOCAL_CREDS and LOCAL_CREDS_PERSISTENT can be used with SOCK_DGRAM, SOCK_STREAM sockets, equivalent to Linux/DragonFlyBSD SO_PASSCRED, while LOCAL_CREDS sends the credentials at first read, LOCAL_CRE…
send_fds
Send the list of file descriptors fds over an AF_UNIX socket.
setdefaulttimeout
Set the default timeout in seconds (real number) for new socket objects.
sethostname
Sets the hostname to name.
SHUT_RD
These constants are used by the shutdown() method of socket objects.
SHUT_RDWR
These constants are used by the shutdown() method of socket objects.
SHUT_WR
These constants are used by the shutdown() method of socket objects.
SIO_KEEPALIVE_VALS
Constants for Windows’ WSAIoctl().
SIO_LOOPBACK_FAST_PATH
Constants for Windows’ WSAIoctl().
SIO_RCVALL
Constants for Windows’ WSAIoctl().
SO_HCI_EVT_FILTER
Option names for use with BTPROTO_HCI.
SO_HCI_PKT_FILTER
Option names for use with BTPROTO_HCI.
SO_INCOMING_CPU
Constant to optimize CPU locality, to be used in conjunction with SO_REUSEPORT.
SO_REUSEPORT_LB
Constant to enable duplicate address and port bindings with load balancing.
SOCK_CLOEXEC
These two constants, if defined, can be combined with the socket types and allow you to set some flags atomically (thus avoiding possible race conditions and the need for separate calls).
SOCK_DGRAM
These constants represent the socket types, used for the second argument to socket().
SOCK_NONBLOCK
These two constants, if defined, can be combined with the socket types and allow you to set some flags atomically (thus avoiding possible race conditions and the need for separate calls).
SOCK_RAW
These constants represent the socket types, used for the second argument to socket().
SOCK_RDM
These constants represent the socket types, used for the second argument to socket().
SOCK_SEQPACKET
These constants represent the socket types, used for the second argument to socket().
SOCK_STREAM
These constants represent the socket types, used for the second argument to socket().
socket
A subclass of _socket.socket adding the makefile() method.
socketpair
Create a pair of socket objects from the sockets returned by the platform socketpair() function.
SocketType
Open a socket of the given type.
SOL_ALG
Constants for Linux Kernel cryptography.
SOL_BLUETOOTH
Used in the level argument to the setsockopt() and getsockopt() methods of Bluetooth socket objects.
SOL_HCI
Used in the level argument to the setsockopt() and getsockopt() methods of Bluetooth socket objects.
SOL_L2CAP
Used in the level argument to the setsockopt() and getsockopt() methods of Bluetooth socket objects.
SOL_RDS
Many constants of these forms, documented in the Linux documentation, are also defined in the socket module.
SOL_RFCOMM
Used in the level argument to the setsockopt() and getsockopt() methods of Bluetooth socket objects.
SOL_SCO
Used in the level argument to the setsockopt() and getsockopt() methods of Bluetooth socket objects.
SOMAXCONN
Many constants of these forms, documented in the Unix documentation on sockets and/or the IP protocol, are also defined in the socket module.

t

timeout
Timeout expired.