2 from _typeshed import ReadableBuffer, WriteableBuffer
3 from collections.abc import Iterable
4 from typing import Any, SupportsInt, Tuple, Union, overload
6 if sys.version_info >= (3, 8):
7 from typing import SupportsIndex
13 _CMSG = Tuple[int, int, bytes]
14 _CMSGArg = Tuple[int, int, ReadableBuffer]
16 # Addresses can be either tuples of varying lengths (AF_INET, AF_INET6,
17 # AF_NETLINK, AF_TIPC) or strings (AF_UNIX).
18 _Address = Union[Tuple[Any, ...], str]
20 # TODO Most methods allow bytes as address objects
22 # ----- Constants -----
23 # Some socket families are listed in the "Socket families" section of the docs,
24 # but not the "Constants" section. These are listed at the end of the list of
27 # Besides those and the first few constants listed, the constants are listed in
28 # documentation order.
32 # Per socketmodule.c, only these three families are portable
43 if sys.platform == "linux":
47 # Address families not mentioned in the docs
73 # The "many constants" referenced by the docs
100 INADDR_ALLHOSTS_GROUP: int
102 INADDR_BROADCAST: int
104 INADDR_MAX_LOCAL_GROUP: int
106 INADDR_UNSPEC_GROUP: int
108 IPPORT_USERRESERVED: int
115 IPPROTO_FRAGMENT: int
150 IPV6_LEAVE_GROUP: int
151 IPV6_MULTICAST_HOPS: int
152 IPV6_MULTICAST_IF: int
153 IPV6_MULTICAST_LOOP: int
157 IPV6_RECVDSTOPTS: int
158 IPV6_RECVHOPLIMIT: int
159 IPV6_RECVHOPOPTS: int
160 IPV6_RECVPATHMTU: int
161 IPV6_RECVPKTINFO: int
165 IPV6_RTHDRDSTOPTS: int
166 IPV6_RTHDR_TYPE_0: int
168 IPV6_UNICAST_HOPS: int
169 IPV6_USE_MIN_MTU: int
172 IP_ADD_MEMBERSHIP: int
173 IP_DEFAULT_MULTICAST_LOOP: int
174 IP_DEFAULT_MULTICAST_TTL: int
175 IP_DROP_MEMBERSHIP: int
177 IP_MAX_MEMBERSHIPS: int
179 IP_MULTICAST_LOOP: int
180 IP_MULTICAST_TTL: int
192 MSG_CMSG_CLOEXEC: int
205 MSG_NOTIFICATION: int
239 SO_EXCLUSIVEADDRUSE: int
259 TCP_DEFER_ACCEPT: int
270 TCP_WINDOW_CLAMP: int
271 if sys.version_info >= (3, 7):
272 TCP_NOTSENT_LOWAT: int
274 # Specifically-documented constants
276 if sys.platform == "linux":
286 CAN_RAW_ERR_FILTER: int
288 CAN_RAW_LOOPBACK: int
289 CAN_RAW_RECV_OWN_MSGS: int
294 CAN_BCM_TX_SETUP: int
295 CAN_BCM_TX_DELETE: int
298 CAN_BCM_RX_SETUP: int
299 CAN_BCM_RX_DELETE: int
301 CAN_BCM_TX_STATUS: int
302 CAN_BCM_TX_EXPIRED: int
303 CAN_BCM_RX_STATUS: int
304 CAN_BCM_RX_TIMEOUT: int
305 CAN_BCM_RX_CHANGED: int
307 CAN_RAW_FD_FRAMES: int
309 if sys.platform == "linux" and sys.version_info >= (3, 8):
310 CAN_BCM_SETTIMER: int
311 CAN_BCM_STARTTIMER: int
312 CAN_BCM_TX_COUNTEVT: int
313 CAN_BCM_TX_ANNOUNCE: int
314 CAN_BCM_TX_CP_CAN_ID: int
315 CAN_BCM_RX_FILTER_ID: int
316 CAN_BCM_RX_CHECK_DLC: int
317 CAN_BCM_RX_NO_AUTOTIMER: int
318 CAN_BCM_RX_ANNOUNCE_RESUME: int
319 CAN_BCM_TX_RESET_MULTI_IDX: int
320 CAN_BCM_RX_RTR_FRAME: int
321 CAN_BCM_CAN_FD_FRAME: int
323 if sys.platform == "linux" and sys.version_info >= (3, 7):
326 if sys.platform == "linux" and sys.version_info >= (3, 9):
328 CAN_RAW_JOIN_FILTERS: int
330 J1939_MAX_UNICAST_ADDR: int
334 J1939_PGN_REQUEST: int
335 J1939_PGN_ADDRESS_CLAIMED: int
336 J1939_PGN_ADDRESS_COMMANDED: int
337 J1939_PGN_PDU1_MAX: int
342 SO_J1939_PROMISC: int
343 SO_J1939_SEND_PRIO: int
344 SO_J1939_ERRQUEUE: int
346 SCM_J1939_DEST_ADDR: int
347 SCM_J1939_DEST_NAME: int
349 SCM_J1939_ERRQUEUE: int
352 J1939_NLA_BYTES_ACKED: int
354 J1939_EE_INFO_NONE: int
355 J1939_EE_INFO_TX_ABORT: int
357 J1939_FILTER_MAX: int
359 if sys.platform == "linux":
362 PACKET_BROADCAST: int
363 PACKET_FASTROUTE: int
366 PACKET_MULTICAST: int
367 PACKET_OTHERHOST: int
370 if sys.platform == "linux":
374 RDS_CANCEL_SENT_TO: int
375 RDS_CMSG_RDMA_ARGS: int
376 RDS_CMSG_RDMA_DEST: int
377 RDS_CMSG_RDMA_MAP: int
378 RDS_CMSG_RDMA_STATUS: int
379 RDS_CMSG_RDMA_UPDATE: int
380 RDS_CONG_MONITOR: int
383 RDS_GET_MR_FOR_DEST: int
384 RDS_RDMA_DONTWAIT: int
386 RDS_RDMA_INVALIDATE: int
387 RDS_RDMA_NOTIFY_ME: int
388 RDS_RDMA_READWRITE: int
390 RDS_RDMA_USE_ONCE: int
393 if sys.platform == "win32":
395 SIO_KEEPALIVE_VALS: int
396 SIO_LOOPBACK_FAST_PATH: int
401 RCVALL_SOCKETLEVELONLY: int
403 if sys.platform == "linux":
408 TIPC_ADDR_NAMESEQ: int
410 TIPC_CLUSTER_SCOPE: int
411 TIPC_CONN_TIMEOUT: int
412 TIPC_CRITICAL_IMPORTANCE: int
413 TIPC_DEST_DROPPABLE: int
414 TIPC_HIGH_IMPORTANCE: int
416 TIPC_LOW_IMPORTANCE: int
417 TIPC_MEDIUM_IMPORTANCE: int
420 TIPC_SRC_DROPPABLE: int
421 TIPC_SUBSCR_TIMEOUT: int
424 TIPC_SUB_SERVICE: int
426 TIPC_WAIT_FOREVER: int
430 if sys.platform == "linux":
437 ALG_SET_AEAD_ASSOCLEN: int
438 ALG_SET_AEAD_AUTHSIZE: int
444 if sys.platform == "linux" and sys.version_info >= (3, 7):
446 IOCTL_VM_SOCKETS_GET_LOCAL_CID: int
450 SO_VM_SOCKETS_BUFFER_MAX_SIZE: int
451 SO_VM_SOCKETS_BUFFER_SIZE: int
452 SO_VM_SOCKETS_BUFFER_MIN_SIZE: int
453 VM_SOCKETS_INVALID_VERSION: int
455 AF_LINK: int # Availability: BSD, macOS
457 # BDADDR_* and HCI_* listed with other bluetooth constants below
464 TCP_USER_TIMEOUT: int
466 if sys.platform == "linux" and sys.version_info >= (3, 8):
469 # Semi-documented constants
470 # (Listed under "Socket families" in the docs, but not "Constants")
472 if sys.platform == "linux":
473 # Netlink is defined by Linux
478 NETLINK_FIREWALL: int
486 NETLINK_USERSOCK: int
490 if sys.platform != "win32" and sys.platform != "darwin":
491 # Linux and some BSD support is explicit in the docs
492 # Windows and macOS do not support in practice
497 BTPROTO_SCO: int # not in FreeBSD
502 HCI_FILTER: int # not in NetBSD or DragonFlyBSD
503 # not in FreeBSD, NetBSD, or DragonFlyBSD
507 if sys.platform == "darwin":
508 # PF_SYSTEM is defined by macOS
510 SYSPROTO_CONTROL: int
512 # ----- Exceptions -----
516 class herror(error): ...
517 class gaierror(error): ...
519 if sys.version_info >= (3, 10):
520 timeout = TimeoutError
522 class timeout(error): ...
524 # ----- Classes -----
530 def __init__(self, family: int = ..., type: int = ..., proto: int = ..., fileno: _FD | None = ...) -> None: ...
531 def bind(self, __address: _Address | bytes) -> None: ...
532 def close(self) -> None: ...
533 def connect(self, __address: _Address | bytes) -> None: ...
534 def connect_ex(self, __address: _Address | bytes) -> int: ...
535 def detach(self) -> int: ...
536 def fileno(self) -> int: ...
537 def getpeername(self) -> _RetAddress: ...
538 def getsockname(self) -> _RetAddress: ...
540 def getsockopt(self, __level: int, __optname: int) -> int: ...
542 def getsockopt(self, __level: int, __optname: int, __buflen: int) -> bytes: ...
543 if sys.version_info >= (3, 7):
544 def getblocking(self) -> bool: ...
545 def gettimeout(self) -> float | None: ...
546 if sys.platform == "win32":
547 def ioctl(self, __control: int, __option: int | tuple[int, int, int] | bool) -> None: ...
548 def listen(self, __backlog: int = ...) -> None: ...
549 def recv(self, __bufsize: int, __flags: int = ...) -> bytes: ...
550 def recvfrom(self, __bufsize: int, __flags: int = ...) -> tuple[bytes, _RetAddress]: ...
551 if sys.platform != "win32":
552 def recvmsg(self, __bufsize: int, __ancbufsize: int = ..., __flags: int = ...) -> tuple[bytes, list[_CMSG], int, Any]: ...
554 self, __buffers: Iterable[WriteableBuffer], __ancbufsize: int = ..., __flags: int = ...
555 ) -> tuple[int, list[_CMSG], int, Any]: ...
556 def recvfrom_into(self, buffer: WriteableBuffer, nbytes: int = ..., flags: int = ...) -> tuple[int, _RetAddress]: ...
557 def recv_into(self, buffer: WriteableBuffer, nbytes: int = ..., flags: int = ...) -> int: ...
558 def send(self, __data: ReadableBuffer, __flags: int = ...) -> int: ...
559 def sendall(self, __data: ReadableBuffer, __flags: int = ...) -> None: ...
561 def sendto(self, __data: ReadableBuffer, __address: _Address) -> int: ...
563 def sendto(self, __data: ReadableBuffer, __flags: int, __address: _Address) -> int: ...
564 if sys.platform != "win32":
567 __buffers: Iterable[ReadableBuffer],
568 __ancdata: Iterable[_CMSGArg] = ...,
570 __address: _Address = ...,
572 if sys.platform == "linux":
574 self, msg: Iterable[ReadableBuffer] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ...
576 def setblocking(self, __flag: bool) -> None: ...
577 def settimeout(self, __value: float | None) -> None: ...
579 def setsockopt(self, __level: int, __optname: int, __value: int | bytes) -> None: ...
581 def setsockopt(self, __level: int, __optname: int, __value: None, __optlen: int) -> None: ...
582 if sys.platform == "win32":
583 def share(self, __process_id: int) -> bytes: ...
584 def shutdown(self, __how: int) -> None: ...
588 # ----- Functions -----
590 if sys.version_info >= (3, 7):
591 def close(__fd: _FD) -> None: ...
593 def dup(__fd: _FD) -> int: ...
595 # the 5th tuple item is an address
597 host: bytes | str | None,
598 port: bytes | str | int | None,
603 ) -> list[tuple[int, int, int, str, tuple[str, int] | tuple[str, int, int, int]]]: ...
604 def gethostbyname(__hostname: str) -> str: ...
605 def gethostbyname_ex(__hostname: str) -> tuple[str, list[str], list[str]]: ...
606 def gethostname() -> str: ...
607 def gethostbyaddr(__ip_address: str) -> tuple[str, list[str], list[str]]: ...
608 def getnameinfo(__sockaddr: tuple[str, int] | tuple[str, int, int, int], __flags: int) -> tuple[str, str]: ...
609 def getprotobyname(__protocolname: str) -> int: ...
610 def getservbyname(__servicename: str, __protocolname: str = ...) -> int: ...
611 def getservbyport(__port: int, __protocolname: str = ...) -> str: ...
612 def ntohl(__x: int) -> int: ... # param & ret val are 32-bit ints
613 def ntohs(__x: int) -> int: ... # param & ret val are 16-bit ints
614 def htonl(__x: int) -> int: ... # param & ret val are 32-bit ints
615 def htons(__x: int) -> int: ... # param & ret val are 16-bit ints
616 def inet_aton(__ip_string: str) -> bytes: ... # ret val 4 bytes in length
617 def inet_ntoa(__packed_ip: bytes) -> str: ...
618 def inet_pton(__address_family: int, __ip_string: str) -> bytes: ...
619 def inet_ntop(__address_family: int, __packed_ip: bytes) -> str: ...
620 def CMSG_LEN(__length: int) -> int: ...
621 def CMSG_SPACE(__length: int) -> int: ...
622 def getdefaulttimeout() -> float | None: ...
623 def setdefaulttimeout(__timeout: float | None) -> None: ...
624 def socketpair(__family: int = ..., __type: int = ..., __proto: int = ...) -> tuple[socket, socket]: ...
626 if sys.platform != "win32":
627 def sethostname(__name: str) -> None: ...
629 # Windows added these in 3.8, but didn't have them before
630 if sys.platform != "win32" or sys.version_info >= (3, 8):
631 def if_nameindex() -> list[tuple[int, str]]: ...
632 def if_nametoindex(__name: str) -> int: ...
633 def if_indextoname(__index: int) -> str: ...