mirror of
https://github.com/jupyter/docker-stacks.git
synced 2025-10-07 18:14:05 +00:00
13 lines
477 B
Python
13 lines
477 B
Python
# Copyright (c) Jupyter Development Team.
|
|
# Distributed under the terms of the Modified BSD License.
|
|
import socket
|
|
from contextlib import closing
|
|
|
|
|
|
def find_free_port() -> str:
|
|
"""Returns the available host port. Can be called in multiple threads/processes."""
|
|
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|
|
s.bind(("", 0))
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
return s.getsockname()[1] # type: ignore
|