13
13
socketpair() -- create a pair of new socket objects [*]
14
14
fromfd() -- create a socket object from an open file descriptor [*]
15
15
send_fds() -- Send file descriptor to the socket.
16
- recv_fds() -- Recieve file descriptors from the socket.
16
+ recv_fds() -- Receive file descriptors from the socket.
17
17
fromshare() -- create a socket object from data received from socket.share() [*]
18
18
gethostname() -- return the current hostname
19
19
gethostbyname() -- map a hostname to its IP number
28
28
socket.setdefaulttimeout() -- set the default timeout value
29
29
create_connection() -- connects to an address, with an optional timeout and
30
30
optional source address.
31
+ create_server() -- create a TCP socket and bind it to a specified address.
31
32
32
33
[*] not available on all platforms!
33
34
@@ -122,7 +123,7 @@ def _intenum_converter(value, enum_klass):
122
123
errorTab [10014 ] = "A fault occurred on the network??" # WSAEFAULT
123
124
errorTab [10022 ] = "An invalid operation was attempted."
124
125
errorTab [10024 ] = "Too many open files."
125
- errorTab [10035 ] = "The socket operation would block"
126
+ errorTab [10035 ] = "The socket operation would block. "
126
127
errorTab [10036 ] = "A blocking operation is already in progress."
127
128
errorTab [10037 ] = "Operation already in progress."
128
129
errorTab [10038 ] = "Socket operation on nonsocket."
@@ -254,17 +255,18 @@ def __repr__(self):
254
255
self .type ,
255
256
self .proto )
256
257
if not closed :
258
+ # getsockname and getpeername may not be available on WASI.
257
259
try :
258
260
laddr = self .getsockname ()
259
261
if laddr :
260
262
s += ", laddr=%s" % str (laddr )
261
- except error :
263
+ except ( error , AttributeError ) :
262
264
pass
263
265
try :
264
266
raddr = self .getpeername ()
265
267
if raddr :
266
268
s += ", raddr=%s" % str (raddr )
267
- except error :
269
+ except ( error , AttributeError ) :
268
270
pass
269
271
s += '>'
270
272
return s
@@ -380,7 +382,7 @@ def _sendfile_use_sendfile(self, file, offset=0, count=None):
380
382
if timeout and not selector_select (timeout ):
381
383
raise TimeoutError ('timed out' )
382
384
if count :
383
- blocksize = count - total_sent
385
+ blocksize = min ( count - total_sent , blocksize )
384
386
if blocksize <= 0 :
385
387
break
386
388
try :
@@ -783,11 +785,11 @@ def getfqdn(name=''):
783
785
784
786
First the hostname returned by gethostbyaddr() is checked, then
785
787
possibly existing aliases. In case no FQDN is available and `name`
786
- was given, it is returned unchanged. If `name` was empty or '0.0.0.0',
788
+ was given, it is returned unchanged. If `name` was empty, '0.0.0.0' or ':: ',
787
789
hostname from gethostname() is returned.
788
790
"""
789
791
name = name .strip ()
790
- if not name or name == '0.0.0.0' :
792
+ if not name or name in ( '0.0.0.0' , '::' ) :
791
793
name = gethostname ()
792
794
try :
793
795
hostname , aliases , ipaddrs = gethostbyaddr (name )
@@ -806,7 +808,7 @@ def getfqdn(name=''):
806
808
_GLOBAL_DEFAULT_TIMEOUT = object ()
807
809
808
810
def create_connection (address , timeout = _GLOBAL_DEFAULT_TIMEOUT ,
809
- source_address = None ):
811
+ source_address = None , * , all_errors = False ):
810
812
"""Connect to *address* and return the socket object.
811
813
812
814
Convenience function. Connect to *address* (a 2-tuple ``(host,
@@ -816,11 +818,13 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
816
818
global default timeout setting returned by :func:`getdefaulttimeout`
817
819
is used. If *source_address* is set it must be a tuple of (host, port)
818
820
for the socket to bind as a source address before making the connection.
819
- A host of '' or port 0 tells the OS to use the default.
821
+ A host of '' or port 0 tells the OS to use the default. When a connection
822
+ cannot be created, raises the last error if *all_errors* is False,
823
+ and an ExceptionGroup of all errors if *all_errors* is True.
820
824
"""
821
825
822
826
host , port = address
823
- err = None
827
+ exceptions = []
824
828
for res in getaddrinfo (host , port , 0 , SOCK_STREAM ):
825
829
af , socktype , proto , canonname , sa = res
826
830
sock = None
@@ -832,20 +836,24 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
832
836
sock .bind (source_address )
833
837
sock .connect (sa )
834
838
# Break explicitly a reference cycle
835
- err = None
839
+ exceptions . clear ()
836
840
return sock
837
841
838
- except error as _ :
839
- err = _
842
+ except error as exc :
843
+ if not all_errors :
844
+ exceptions .clear () # raise only the last error
845
+ exceptions .append (exc )
840
846
if sock is not None :
841
847
sock .close ()
842
848
843
- if err is not None :
849
+ if len ( exceptions ) :
844
850
try :
845
- raise err
851
+ if not all_errors :
852
+ raise exceptions [0 ]
853
+ raise ExceptionGroup ("create_connection failed" , exceptions )
846
854
finally :
847
855
# Break explicitly a reference cycle
848
- err = None
856
+ exceptions . clear ()
849
857
else :
850
858
raise error ("getaddrinfo returns an empty list" )
851
859
@@ -902,7 +910,7 @@ def create_server(address, *, family=AF_INET, backlog=None, reuse_port=False,
902
910
# address, effectively preventing this one from accepting
903
911
# connections. Also, it may set the process in a state where
904
912
# it'll no longer respond to any signals or graceful kills.
905
- # See: msdn2 .microsoft.com/en-us/library/ms740621(VS.85).aspx
913
+ # See: https://learn .microsoft.com/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
906
914
if os .name not in ('nt' , 'cygwin' ) and \
907
915
hasattr (_socket , 'SO_REUSEADDR' ):
908
916
try :
0 commit comments