Home | History | Annotate | Download | only in utils_lib

Lines Matching refs:port

19     """Finds a semi-random available port.
21 A race condition is still possible after the port number is returned, if
25 A port number that is unused on both TCP and UDP.
28 # same port over and over. So always try TCP first.
30 # Ask the OS for an unused port.
31 port = _try_bind(0, socket.SOCK_STREAM, socket.IPPROTO_TCP)
32 # Check if this port is unused on the other protocol.
33 if port and _try_bind(port, socket.SOCK_DGRAM, socket.IPPROTO_UDP):
34 return port
37 def is_port_available(port):
38 """Checks if a given port number is available on the system.
41 port: An integer which is the port number to check.
44 True if the port is available; False otherwise.
46 return (_try_bind(port, socket.SOCK_STREAM, socket.IPPROTO_TCP) and
47 _try_bind(port, socket.SOCK_DGRAM, socket.IPPROTO_UDP))
50 def _try_bind(port, socket_type, socket_proto):
54 s.bind(('', port))
56 # IPv4 and IPv6 the second field is a port number.