mirror of https://github.com/python/cpython
Issue #12054: use support.find_unused_port() instead of reinventing the wheel
This commit is contained in:
commit
74b4885cc9
|
@ -567,23 +567,9 @@ class GeneralModuleTests(unittest.TestCase):
|
||||||
|
|
||||||
# XXX The following don't test module-level functionality...
|
# XXX The following don't test module-level functionality...
|
||||||
|
|
||||||
def _get_unused_port(self, bind_address='0.0.0.0'):
|
|
||||||
"""Use a temporary socket to elicit an unused ephemeral port.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
bind_address: Hostname or IP address to search for a port on.
|
|
||||||
|
|
||||||
Returns: A most likely to be unused port.
|
|
||||||
"""
|
|
||||||
tempsock = socket.socket()
|
|
||||||
tempsock.bind((bind_address, 0))
|
|
||||||
host, port = tempsock.getsockname()
|
|
||||||
tempsock.close()
|
|
||||||
return port
|
|
||||||
|
|
||||||
def testSockName(self):
|
def testSockName(self):
|
||||||
# Testing getsockname()
|
# Testing getsockname()
|
||||||
port = self._get_unused_port()
|
port = support.find_unused_port()
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.addCleanup(sock.close)
|
self.addCleanup(sock.close)
|
||||||
sock.bind(("0.0.0.0", port))
|
sock.bind(("0.0.0.0", port))
|
||||||
|
@ -632,7 +618,7 @@ class GeneralModuleTests(unittest.TestCase):
|
||||||
|
|
||||||
def test_getsockaddrarg(self):
|
def test_getsockaddrarg(self):
|
||||||
host = '0.0.0.0'
|
host = '0.0.0.0'
|
||||||
port = self._get_unused_port(bind_address=host)
|
port = support.find_unused_port()
|
||||||
big_port = port + 65536
|
big_port = port + 65536
|
||||||
neg_port = port - 65536
|
neg_port = port - 65536
|
||||||
sock = socket.socket()
|
sock = socket.socket()
|
||||||
|
|
Loading…
Reference in New Issue