• Home
  • Raw
  • Download

Lines Matching full:socket

3 import socket
18 def find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM):
20 achieved by creating a temporary socket with the same family and type as
23 eliciting an unused ephemeral port from the OS. The temporary socket is
27 server socket needs to be bound to a particular port for the duration of
29 a python socket, or if an unused port needs to be provided in a constructor
33 socket is bound to a hard coded port, the ability to run multiple instances
42 the SO_REUSEADDR socket option having different semantics on Windows versus
46 the order bind and listen were called on each socket).
50 accept() is called on each socket, the second caller's process will steal
55 The solution on Windows is to use the SO_EXCLUSIVEADDRUSE socket option
69 other process when we close and delete our temporary socket but before our
74 with socket.socket(family, socktype) as tempsock:
80 """Bind the socket to a free port and return the port number. Relies on
84 is AF_INET and sock.type is SOCK_STREAM, *and* the socket has SO_REUSEADDR
85 or SO_REUSEPORT set on it. Tests should *never* set these socket options
89 Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e.
90 on Windows), it will be set on the socket. This will prevent anyone else
94 if sock.family == socket.AF_INET and sock.type == socket.SOCK_STREAM:
95 if hasattr(socket, 'SO_REUSEADDR'):
96 if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) == 1:
98 "SO_REUSEADDR socket option on "
100 if hasattr(socket, 'SO_REUSEPORT'):
102 if sock.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT) == 1:
104 "SO_REUSEPORT socket option on "
107 # Python's socket module was compiled using modern headers
111 if hasattr(socket, 'SO_EXCLUSIVEADDRUSE'):
112 sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
119 """Bind a unix socket, raising SkipTest if PermissionError is raised."""
120 assert sock.family == socket.AF_UNIX
129 if socket.has_ipv6:
132 sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
148 if not hasattr(socket, 'AF_UNIX'):
154 with socket.socket(socket.AF_UNIX) as sock:
171 Get the different socket error numbers ('errno') which can be received
179 # bpo-31910: socket.create_connection() fails randomly
207 # socket.create_connection() fails randomly with
226 gai_errnos = [getattr(socket, name, num)
232 (isinstance(err, socket.gaierror) and n in gai_errnos) or
244 old_timeout = socket.getdefaulttimeout()
247 socket.setdefaulttimeout(timeout)
254 # urllib can wrap original socket errors multiple times (!), we must
261 # except socket.error as msg:
262 # raise OSError('socket error', msg) from msg
272 socket.setdefaulttimeout(old_timeout)