Unverified Commit a8480911 authored by YH's avatar YH Committed by GitHub
Browse files

Fix port exception type (#2925)

parent 61e68783
...@@ -50,16 +50,20 @@ def ensure_path_exists(filename: str): ...@@ -50,16 +50,20 @@ def ensure_path_exists(filename: str):
Path(dirpath).mkdir(parents=True, exist_ok=True) Path(dirpath).mkdir(parents=True, exist_ok=True)
def free_port(): def free_port() -> int:
"""Get a free port on localhost.
Returns:
int: A free port on localhost.
"""
while True: while True:
port = random.randint(20000, 65000)
try: try:
sock = socket.socket() with socket.socket() as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
port = random.randint(20000, 65000) sock.bind(("localhost", port))
sock.bind(('localhost', port))
sock.close()
return port return port
except Exception: except OSError:
continue continue
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment