1
# Wrapper module for _socket, providing some additional facilities
2
# implemented in Python.
5
This module provides socket operations and some related functions.
6
On Unix, it supports IP (Internet Protocol) and Unix domain sockets.
7
On other systems, it only supports IP. Functions specific for a
8
socket are available as methods of the socket object.
12
socket() -- create a new socket object
13
socketpair() -- create a pair of new socket objects [*]
14
fromfd() -- create a socket object from an open file descriptor [*]
15
send_fds() -- Send file descriptor to the socket.
16
recv_fds() -- Receive file descriptors from the socket.
17
fromshare() -- create a socket object from data received from socket.share() [*]
18
gethostname() -- return the current hostname
19
gethostbyname() -- map a hostname to its IP number
20
gethostbyaddr() -- map an IP number or hostname to DNS info
21
getservbyname() -- map a service name and a protocol name to a port number
22
getprotobyname() -- map a protocol name (e.g. 'tcp') to a number
23
ntohs(), ntohl() -- convert 16, 32 bit int from network to host byte order
24
htons(), htonl() -- convert 16, 32 bit int from host to network byte order
25
inet_aton() -- convert IP addr string (123.45.67.89) to 32-bit packed format
26
inet_ntoa() -- convert 32-bit packed format IP to string (123.45.67.89)
27
socket.getdefaulttimeout() -- get the default timeout value
28
socket.setdefaulttimeout() -- set the default timeout value
29
create_connection() -- connects to an address, with an optional timeout and
30
optional source address.
31
create_server() -- create a TCP socket and bind it to a specified address.
33
[*] not available on all platforms!
37
SocketType -- type object for socket objects
38
error -- exception raised for I/O errors
39
has_ipv6 -- boolean value indicating if IPv6 is supported
43
AF_INET, AF_UNIX -- socket domains (first argument to socket() call)
44
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW -- socket types (second argument)
48
Many other constants may be defined; these may be used in calls to
49
the setsockopt() and getsockopt() methods.
58
from enum import IntEnum, IntFlag
59
from functools import partial
65
EBADF = getattr(errno, 'EBADF', 9)
66
EAGAIN = getattr(errno, 'EAGAIN', 11)
67
EWOULDBLOCK = getattr(errno, 'EWOULDBLOCK', 11)
69
__all__ = ["fromfd", "getfqdn", "create_connection", "create_server",
70
"has_dualstack_ipv6", "AddressFamily", "SocketKind"]
71
__all__.extend(os._get_exports_list(_socket))
73
# Set up the socket.AF_* socket.SOCK_* constants as members of IntEnums for
74
# nicer string representations.
75
# Note that _socket only knows about the integer values. The public interface
76
# in this module understands the enums and translates them back from integers
77
# where needed (e.g. .family property of a socket object).
82
lambda C: C.isupper() and C.startswith('AF_'))
87
lambda C: C.isupper() and C.startswith('SOCK_'))
92
lambda C: C.isupper() and C.startswith('MSG_'))
97
lambda C: C.isupper() and C.startswith('AI_'))
99
_LOCALHOST = '127.0.0.1'
100
_LOCALHOST_V6 = '::1'
103
def _intenum_converter(value, enum_klass):
104
"""Convert a numeric family value to an IntEnum member.
106
If it's not a known member, return the numeric value itself.
109
return enum_klass(value)
115
if sys.platform.lower().startswith("win"):
117
6: "Specified event object handle is invalid.",
118
8: "Insufficient memory available.",
119
87: "One or more parameters are invalid.",
120
995: "Overlapped operation aborted.",
121
996: "Overlapped I/O event object not in signaled state.",
122
997: "Overlapped operation will complete later.",
123
10004: "The operation was interrupted.",
124
10009: "A bad file handle was passed.",
125
10013: "Permission denied.",
126
10014: "An invalid pointer was passed.",
127
10022: "An invalid operation was attempted.",
128
10024: "Too many open files.",
129
10035: "The socket operation would block.",
130
10036: "A blocking operation is already in progress.",
131
10037: "Operation already in progress.",
132
10038: "Socket operation on nonsocket.",
133
10039: "Destination address required.",
134
10040: "Message too long.",
135
10041: "Protocol wrong type for socket.",
136
10042: "Bad protocol option.",
137
10043: "Protocol not supported.",
138
10044: "Socket type not supported.",
139
10045: "Operation not supported.",
140
10046: "Protocol family not supported.",
141
10047: "Address family not supported by protocol family.",
142
10048: "The network address is in use.",
143
10049: "Cannot assign requested address.",
144
10050: "Network is down.",
145
10051: "Network is unreachable.",
146
10052: "Network dropped connection on reset.",
147
10053: "Software caused connection abort.",
148
10054: "The connection has been reset.",
149
10055: "No buffer space available.",
150
10056: "Socket is already connected.",
151
10057: "Socket is not connected.",
152
10058: "The network has been shut down.",
153
10059: "Too many references.",
154
10060: "The operation timed out.",
155
10061: "Connection refused.",
156
10062: "Cannot translate name.",
157
10063: "The name is too long.",
158
10064: "The host is down.",
159
10065: "The host is unreachable.",
160
10066: "Directory not empty.",
161
10067: "Too many processes.",
162
10068: "User quota exceeded.",
163
10069: "Disk quota exceeded.",
164
10070: "Stale file handle reference.",
165
10071: "Item is remote.",
166
10091: "Network subsystem is unavailable.",
167
10092: "Winsock.dll version out of range.",
168
10093: "Successful WSAStartup not yet performed.",
169
10101: "Graceful shutdown in progress.",
170
10102: "No more results from WSALookupServiceNext.",
171
10103: "Call has been canceled.",
172
10104: "Procedure call table is invalid.",
173
10105: "Service provider is invalid.",
174
10106: "Service provider failed to initialize.",
175
10107: "System call failure.",
176
10108: "Service not found.",
177
10109: "Class type not found.",
178
10110: "No more results from WSALookupServiceNext.",
179
10111: "Call was canceled.",
180
10112: "Database query was refused.",
181
11001: "Host not found.",
182
11002: "Nonauthoritative host not found.",
183
11003: "This is a nonrecoverable error.",
184
11004: "Valid name, no data record of requested type.",
185
11005: "QoS receivers.",
186
11006: "QoS senders.",
187
11007: "No QoS senders.",
188
11008: "QoS no receivers.",
189
11009: "QoS request confirmed.",
190
11010: "QoS admission error.",
191
11011: "QoS policy failure.",
192
11012: "QoS bad style.",
193
11013: "QoS bad object.",
194
11014: "QoS traffic control error.",
195
11015: "QoS generic error.",
196
11016: "QoS service type error.",
197
11017: "QoS flowspec error.",
198
11018: "Invalid QoS provider buffer.",
199
11019: "Invalid QoS filter style.",
200
11020: "Invalid QoS filter type.",
201
11021: "Incorrect QoS filter count.",
202
11022: "Invalid QoS object length.",
203
11023: "Incorrect QoS flow count.",
204
11024: "Unrecognized QoS object.",
205
11025: "Invalid QoS policy object.",
206
11026: "Invalid QoS flow descriptor.",
207
11027: "Invalid QoS provider-specific flowspec.",
208
11028: "Invalid QoS provider-specific filterspec.",
209
11029: "Invalid QoS shape discard mode object.",
210
11030: "Invalid QoS shaping rate object.",
211
11031: "Reserved policy QoS element type."
213
__all__.append("errorTab")
216
class _GiveupOnSendfile(Exception): pass
219
class socket(_socket.socket):
221
"""A subclass of _socket.socket adding the makefile() method."""
223
__slots__ = ["__weakref__", "_io_refs", "_closed"]
225
def __init__(self, family=-1, type=-1, proto=-1, fileno=None):
226
# For user code address family and type values are IntEnum members, but
227
# for the underlying _socket.socket they're just integers. The
228
# constructor of _socket.socket converts the given argument to an
229
# integer automatically.
237
_socket.socket.__init__(self, family, type, proto, fileno)
244
def __exit__(self, *args):
249
"""Wrap __repr__() to reveal the real class name and socket
252
closed = getattr(self, '_closed', False)
253
s = "<%s.%s%s fd=%i, family=%s, type=%s, proto=%i" \
254
% (self.__class__.__module__,
255
self.__class__.__qualname__,
256
" [closed]" if closed else "",
262
# getsockname and getpeername may not be available on WASI.
264
laddr = self.getsockname()
266
s += ", laddr=%s" % str(laddr)
267
except (error, AttributeError):
270
raddr = self.getpeername()
272
s += ", raddr=%s" % str(raddr)
273
except (error, AttributeError):
278
def __getstate__(self):
279
raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
282
"""dup() -> socket object
284
Duplicate the socket. Return a new socket object connected to the same
285
system resource. The new socket is non-inheritable.
287
fd = dup(self.fileno())
288
sock = self.__class__(self.family, self.type, self.proto, fileno=fd)
289
sock.settimeout(self.gettimeout())
293
"""accept() -> (socket object, address info)
295
Wait for an incoming connection. Return a new socket
296
representing the connection, and the address of the client.
297
For IP sockets, the address info is a pair (hostaddr, port).
299
4
4
fd, addr = self._accept()
300
14
14
sock = socket(self.family, self.type, self.proto, fileno=fd)
301
# Issue #7995: if no default timeout is set and the listening
302
# socket had a (non-zero) timeout, force the new socket in blocking
303
# mode to override platform-specific socket flags inheritance.
304
if getdefaulttimeout() is None and self.gettimeout():
305
sock.setblocking(True)
308
def makefile(self, mode="r", buffering=None, *,
309
encoding=None, errors=None, newline=None):
310
"""makefile(...) -> an I/O stream connected to the socket
312
The arguments are as for io.open() after the filename, except the only
313
supported mode values are 'r' (default), 'w', 'b', or a combination of
316
# XXX refactor to share code?
317
if not set(mode) <= {"r", "w", "b"}:
318
raise ValueError("invalid mode %r (only r, w, b allowed)" % (mode,))
319
writing = "w" in mode
320
reading = "r" in mode or not writing
321
assert reading or writing
328
raw = SocketIO(self, rawmode)
330
if buffering is None:
333
buffering = io.DEFAULT_BUFFER_SIZE
336
raise ValueError("unbuffered streams must be binary")
338
if reading and writing:
339
buffer = io.BufferedRWPair(raw, raw, buffering)
341
buffer = io.BufferedReader(raw, buffering)
344
buffer = io.BufferedWriter(raw, buffering)
347
encoding = io.text_encoding(encoding)
348
text = io.TextIOWrapper(buffer, encoding, errors, newline)
352
def _sendfile_zerocopy(self, zerocopy_func, giveup_exc_type, file,
353
offset=0, count=None):
355
Send a file using a zero-copy function.
359
self._check_sendfile_params(file, offset, count)
360
sockno = self.fileno()
362
fileno = file.fileno()
363
except (AttributeError, io.UnsupportedOperation) as err:
364
raise giveup_exc_type(err) # not a regular file
366
fsize = os.fstat(fileno).st_size
367
except OSError as err:
368
raise giveup_exc_type(err) # not a regular file
370
return 0 # empty file
371
# Truncate to 1GiB to avoid OverflowError, see bpo-38319.
372
blocksize = min(count or fsize, 2 ** 30)
373
timeout = self.gettimeout()
375
raise ValueError("non-blocking sockets are not supported")
376
# poll/select have the advantage of not requiring any
377
# extra file descriptor, contrarily to epoll/kqueue
378
# (also, they require a single syscall).
379
if hasattr(selectors, 'PollSelector'):
380
selector = selectors.PollSelector()
382
selector = selectors.SelectSelector()
383
selector.register(sockno, selectors.EVENT_WRITE)
386
# localize variable access to minimize overhead
387
selector_select = selector.select
390
if timeout and not selector_select(timeout):
391
raise TimeoutError('timed out')
393
blocksize = min(count - total_sent, blocksize)
397
sent = zerocopy_func(fileno, offset, blocksize)
398
except BlockingIOError:
400
# Block until the socket is ready to send some
401
# data; avoids hogging CPU resources.
404
except OSError as err:
406
# We can get here for different reasons, the main
407
# one being 'file' is not a regular mmap(2)-like
408
# file, in which case we'll fall back on using
410
raise giveup_exc_type(err)
419
if total_sent > 0 and hasattr(file, 'seek'):
422
if hasattr(os, 'sendfile'):
423
def _sendfile_use_sendfile(self, file, offset=0, count=None):
424
return self._sendfile_zerocopy(
425
partial(os.sendfile, self.fileno()),
430
def _sendfile_use_sendfile(self, file, offset=0, count=None):
431
raise _GiveupOnSendfile(
432
"os.sendfile() not available on this platform")
434
def _sendfile_use_send(self, file, offset=0, count=None):
435
self._check_sendfile_params(file, offset, count)
436
if self.gettimeout() == 0:
437
raise ValueError("non-blocking sockets are not supported")
440
blocksize = min(count, 8192) if count else 8192
442
# localize variable access to minimize overhead
443
file_read = file.read
444
sock_send = self.send
448
blocksize = min(count - total_sent, blocksize)
451
data = memoryview(file_read(blocksize))
456
sent = sock_send(data)
457
except BlockingIOError:
467
if total_sent > 0 and hasattr(file, 'seek'):
468
file.seek(offset + total_sent)
470
def _check_sendfile_params(self, file, offset, count):
471
if 'b' not in getattr(file, 'mode', 'b'):
472
raise ValueError("file should be opened in binary mode")
473
if not self.type & SOCK_STREAM:
474
raise ValueError("only SOCK_STREAM type sockets are supported")
475
if count is not None:
476
if not isinstance(count, int):
478
"count must be a positive integer (got {!r})".format(count))
481
"count must be a positive integer (got {!r})".format(count))
483
def sendfile(self, file, offset=0, count=None):
484
"""sendfile(file[, offset[, count]]) -> sent
486
Send a file until EOF is reached by using high-performance
487
os.sendfile() and return the total number of bytes which
489
*file* must be a regular file object opened in binary mode.
490
If os.sendfile() is not available (e.g. Windows) or file is
491
not a regular file socket.send() will be used instead.
492
*offset* tells from where to start reading the file.
493
If specified, *count* is the total number of bytes to transmit
494
as opposed to sending the file until EOF is reached.
495
File position is updated on return or also in case of error in
496
which case file.tell() can be used to figure out the number of
497
bytes which were sent.
498
The socket must be of SOCK_STREAM type.
499
Non-blocking sockets are not supported.
502
return self._sendfile_use_sendfile(file, offset, count)
503
except _GiveupOnSendfile:
504
return self._sendfile_use_send(file, offset, count)
506
def _decref_socketios(self):
507
if self._io_refs > 0:
512
def _real_close(self, _ss=_socket.socket):
513
# This function should not reference any globals. See issue #808164.
514
4
4
_ss.close(self)
517
# This function should not reference any globals. See issue #808164.
519
if self._io_refs <= 0:
520
5
9
self._real_close()
523
"""detach() -> file descriptor
525
Close the socket object without closing the underlying file descriptor.
526
The object cannot be used after this call, but the file descriptor
527
can be reused for other purposes. The file descriptor is returned.
530
return super().detach()
534
"""Read-only access to the address family for this socket.
536
return _intenum_converter(super().family, AddressFamily)
540
"""Read-only access to the socket type.
542
return _intenum_converter(super().type, SocketKind)
545
def get_inheritable(self):
546
return os.get_handle_inheritable(self.fileno())
547
def set_inheritable(self, inheritable):
548
os.set_handle_inheritable(self.fileno(), inheritable)
550
def get_inheritable(self):
551
return os.get_inheritable(self.fileno())
552
def set_inheritable(self, inheritable):
553
os.set_inheritable(self.fileno(), inheritable)
554
get_inheritable.__doc__ = "Get the inheritable flag of the socket"
555
set_inheritable.__doc__ = "Set the inheritable flag of the socket"
557
def fromfd(fd, family, type, proto=0):
558
""" fromfd(fd, family, type[, proto]) -> socket object
560
Create a socket object from a duplicate of the given file
561
descriptor. The remaining arguments are the same as for socket().
564
return socket(family, type, proto, nfd)
566
if hasattr(_socket.socket, "sendmsg"):
567
def send_fds(sock, buffers, fds, flags=0, address=None):
568
""" send_fds(sock, buffers, fds[, flags[, address]]) -> integer
570
Send the list of file descriptors fds over an AF_UNIX socket.
574
return sock.sendmsg(buffers, [(_socket.SOL_SOCKET,
575
_socket.SCM_RIGHTS, array.array("i", fds))])
576
__all__.append("send_fds")
578
if hasattr(_socket.socket, "recvmsg"):
579
def recv_fds(sock, bufsize, maxfds, flags=0):
580
""" recv_fds(sock, bufsize, maxfds[, flags]) -> (data, list of file
581
descriptors, msg_flags, address)
583
Receive up to maxfds file descriptors returning the message
584
data and a list containing the descriptors.
589
fds = array.array("i")
590
msg, ancdata, flags, addr = sock.recvmsg(bufsize,
591
_socket.CMSG_LEN(maxfds * fds.itemsize))
592
for cmsg_level, cmsg_type, cmsg_data in ancdata:
593
if (cmsg_level == _socket.SOL_SOCKET and cmsg_type == _socket.SCM_RIGHTS):
594
fds.frombytes(cmsg_data[:
595
len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
597
return msg, list(fds), flags, addr
598
__all__.append("recv_fds")
600
if hasattr(_socket.socket, "share"):
602
""" fromshare(info) -> socket object
604
Create a socket object from the bytes object returned by
607
return socket(0, 0, 0, info)
608
__all__.append("fromshare")
610
# Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain.
611
# This is used if _socket doesn't natively provide socketpair. It's
612
# always defined so that it can be patched in for testing purposes.
613
def _fallback_socketpair(family=AF_INET, type=SOCK_STREAM, proto=0):
614
if family == AF_INET:
616
elif family == AF_INET6:
619
raise ValueError("Only AF_INET and AF_INET6 socket address families "
621
if type != SOCK_STREAM:
622
raise ValueError("Only SOCK_STREAM socket type is supported")
624
raise ValueError("Only protocol zero is supported")
626
# We create a connected TCP socket. Note the trick with
627
# setblocking(False) that prevents us from having to create a thread.
628
lsock = socket(family, type, proto)
630
lsock.bind((host, 0))
632
# On IPv6, ignore flow_info and scope_id
633
addr, port = lsock.getsockname()[:2]
634
csock = socket(family, type, proto)
636
csock.setblocking(False)
638
csock.connect((addr, port))
639
except (BlockingIOError, InterruptedError):
641
csock.setblocking(True)
642
ssock, _ = lsock.accept()
649
# Authenticating avoids using a connection from something else
650
# able to connect to {host}:{port} instead of us.
651
# We expect only AF_INET and AF_INET6 families.
653
# Note that we skip this on WASI because on that platorm the client socket
654
# may not have finished connecting by the time we've reached this point (gh-146139).
655
if sys.platform != "wasi":
658
ssock.getsockname() != csock.getpeername()
659
or csock.getsockname() != ssock.getpeername()
661
raise ConnectionError("Unexpected peer connection")
663
# getsockname() and getpeername() can fail
664
# if either socket isn't connected.
669
return (ssock, csock)
671
if hasattr(_socket, "socketpair"):
672
def socketpair(family=None, type=SOCK_STREAM, proto=0):
678
a, b = _socket.socketpair(family, type, proto)
679
a = socket(family, type, proto, a.detach())
680
b = socket(family, type, proto, b.detach())
684
socketpair = _fallback_socketpair
685
__all__.append("socketpair")
687
socketpair.__doc__ = """socketpair([family[, type[, proto]]]) -> (socket object, socket object)
688
Create a pair of socket objects from the sockets returned by the platform
689
socketpair() function.
690
The arguments are the same as for socket() except the default family is AF_UNIX
691
if defined on the platform; otherwise, the default is AF_INET.
694
_blocking_errnos = { EAGAIN, EWOULDBLOCK }
696
class SocketIO(io.RawIOBase):
698
"""Raw I/O implementation for stream sockets.
700
This class supports the makefile() method on sockets. It provides
701
the raw I/O interface on top of a socket object.
704
# One might wonder why not let FileIO do the job instead. There are two
705
# main reasons why FileIO is not adapted:
706
# - it wouldn't work under Windows (where you can't used read() and
707
# write() on a socket handle)
708
# - it wouldn't work with socket timeouts (FileIO would ignore the
709
# timeout and consider the socket non-blocking)
713
def __init__(self, sock, mode):
714
if mode not in ("r", "w", "rw", "rb", "wb", "rwb"):
715
raise ValueError("invalid mode: %r" % mode)
716
io.RawIOBase.__init__(self)
721
self._reading = "r" in mode
722
self._writing = "w" in mode
723
self._timeout_occurred = False
725
def readinto(self, b):
726
"""Read up to len(b) bytes into the writable buffer *b* and return
727
the number of bytes read. If the socket is non-blocking and no bytes
728
are available, None is returned.
730
If *b* is non-empty, a 0 return value indicates that the connection
731
was shutdown at the other end.
734
self._checkReadable()
735
if self._timeout_occurred:
736
raise OSError("cannot read from timed out object")
738
return self._sock.recv_into(b)
740
self._timeout_occurred = True
743
if e.errno in _blocking_errnos:
748
"""Write the given bytes or bytearray object *b* to the socket
749
and return the number of bytes written. This can be less than
750
len(b) if not all data could be written. If the socket is
751
non-blocking and no bytes could be written None is returned.
754
self._checkWritable()
756
return self._sock.send(b)
758
# XXX what about EINTR?
759
if e.errno in _blocking_errnos:
764
"""True if the SocketIO is open for reading.
767
raise ValueError("I/O operation on closed socket.")
771
"""True if the SocketIO is open for writing.
774
raise ValueError("I/O operation on closed socket.")
778
"""True if the SocketIO is open for seeking.
781
raise ValueError("I/O operation on closed socket.")
782
return super().seekable()
785
"""Return the file descriptor of the underlying socket.
788
return self._sock.fileno()
802
"""Close the SocketIO object. This doesn't close the underlying
803
socket, except if all references to it have disappeared.
807
io.RawIOBase.close(self)
808
self._sock._decref_socketios()
812
def getfqdn(name=''):
813
"""Get fully qualified domain name from name.
815
An empty argument is interpreted as meaning the local host.
817
First the hostname returned by gethostbyaddr() is checked, then
818
possibly existing aliases. In case no FQDN is available and `name`
819
was given, it is returned unchanged. If `name` was empty, '0.0.0.0' or '::',
820
hostname from gethostname() is returned.
823
if not name or name in ('0.0.0.0', '::'):
826
hostname, aliases, ipaddrs = gethostbyaddr(name)
830
aliases.insert(0, hostname)
839
_GLOBAL_DEFAULT_TIMEOUT = object()
841
def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
842
source_address=None, *, all_errors=False):
843
"""Connect to *address* and return the socket object.
845
Convenience function. Connect to *address* (a 2-tuple ``(host,
846
port)``) and return the socket object. Passing the optional
847
*timeout* parameter will set the timeout on the socket instance
848
before attempting to connect. If no *timeout* is supplied, the
849
global default timeout setting returned by :func:`getdefaulttimeout`
850
is used. If *source_address* is set it must be a tuple of (host, port)
851
for the socket to bind as a source address before making the connection.
852
A host of '' or port 0 tells the OS to use the default. When a connection
853
cannot be created, raises the last error if *all_errors* is False,
854
and an ExceptionGroup of all errors if *all_errors* is True.
859
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
860
af, socktype, proto, canonname, sa = res
863
sock = socket(af, socktype, proto)
864
if timeout is not _GLOBAL_DEFAULT_TIMEOUT:
865
sock.settimeout(timeout)
867
sock.bind(source_address)
869
# Break explicitly a reference cycle
875
exceptions.clear() # raise only the last error
876
exceptions.append(exc)
884
raise ExceptionGroup("create_connection failed", exceptions)
886
# Break explicitly a reference cycle
889
raise error("getaddrinfo returns an empty list")
892
def has_dualstack_ipv6():
893
"""Return True if the platform supports creating a SOCK_STREAM socket
894
which can handle both AF_INET and AF_INET6 (IPv4 / IPv6) connections.
897
or not hasattr(_socket, 'IPPROTO_IPV6') \
898
or not hasattr(_socket, 'IPV6_V6ONLY'):
901
with socket(AF_INET6, SOCK_STREAM) as sock:
902
sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0)
903
# On some platforms (e.g. DragonFly BSD) setting IPV6_V6ONLY to 0
904
# silently has no effect, so check that it was actually cleared.
905
return sock.getsockopt(IPPROTO_IPV6, IPV6_V6ONLY) == 0
910
def create_server(address, *, family=AF_INET, backlog=None, reuse_port=False,
911
dualstack_ipv6=False):
912
"""Convenience function which creates a SOCK_STREAM type socket
913
bound to *address* (a 2-tuple (host, port)) and return the socket
916
*family* should be either AF_INET or AF_INET6.
917
*backlog* is the queue size passed to socket.listen().
918
*reuse_port* dictates whether to use the SO_REUSEPORT socket option.
919
*dualstack_ipv6*: if true and the platform supports it, it will
920
create an AF_INET6 socket able to accept both IPv4 or IPv6
921
connections. When false it will explicitly disable this option on
922
platforms that enable it by default (e.g. Linux).
924
>>> with create_server(('', 8000)) as server:
926
... conn, addr = server.accept()
927
... # handle new connection
929
if reuse_port and not hasattr(_socket, "SO_REUSEPORT"):
930
raise ValueError("SO_REUSEPORT not supported on this platform")
932
if not has_dualstack_ipv6():
933
raise ValueError("dualstack_ipv6 not supported on this platform")
934
if family != AF_INET6:
935
raise ValueError("dualstack_ipv6 requires AF_INET6 family")
936
sock = socket(family, SOCK_STREAM)
938
# Note about Windows. We don't set SO_REUSEADDR because:
939
# 1) It's unnecessary: bind() will succeed even in case of a
940
# previous closed socket on the same address and still in
942
# 2) If set, another socket is free to bind() on the same
943
# address, effectively preventing this one from accepting
944
# connections. Also, it may set the process in a state where
945
# it'll no longer respond to any signals or graceful kills.
946
# See: https://learn.microsoft.com/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
947
if os.name not in ('nt', 'cygwin') and \
948
hasattr(_socket, 'SO_REUSEADDR'):
950
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
952
# Fail later on bind(), for platforms which may not
953
# support this option.
955
# Since Linux 6.12.9, SO_REUSEPORT is not allowed
956
# on other address families than AF_INET/AF_INET6.
957
if reuse_port and family in (AF_INET, AF_INET6):
958
sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
959
if has_ipv6 and family == AF_INET6:
961
sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 0)
962
elif hasattr(_socket, "IPV6_V6ONLY") and \
963
hasattr(_socket, "IPPROTO_IPV6"):
964
sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, 1)
968
msg = '%s (while attempting to bind on address %r)' % \
969
(err.strerror, address)
970
raise error(err.errno, msg) from None
981
def getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
982
"""Resolve host and port into list of address info entries.
984
Translate the host/port argument into a sequence of 5-tuples that contain
985
all the necessary arguments for creating a socket connected to that service.
986
host is a domain name, a string representation of an IPv4/v6 address or
987
None. port is a string service name such as 'http', a numeric port number or
988
None. By passing None as the value of host and port, you can pass NULL to
989
the underlying C API.
991
The family, type and proto arguments can be optionally specified in order to
992
narrow the list of addresses returned. Passing zero as a value for each of
993
these arguments selects the full range of results.
995
# We override this function since we want to translate the numeric family
996
# and socket type values to enum constants.
998
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
999
af, socktype, proto, canonname, sa = res
1000
addrlist.append((_intenum_converter(af, AddressFamily),
1001
_intenum_converter(socktype, SocketKind),
1002
proto, canonname, sa))