import socket


def _isPortFree(port: int, host: str = "localhost") -> bool:
    """ Return True if port is free, False otherwise.

    Note:
        Only TCP IPv4 port is checked.

    Args:
        port: port number to check.
        host: IP address or 'localhost' where port is `checked`_.

    Returns:
        ``True`` if selected TCP port is free, ``False`` otherwise.

    .. _checked:
        https://stackoverflow.com/questions/2470971/fast-way-to-test-if-a-port-is-in-use-using-python
    """
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socketHandler:
        socketHandler.settimeout(0.2)
        isFree = False
        try:
            socketHandler.bind((host, port))
            isFree = True
        except Exception as err:  # pragma: no cover
            pass

        return isFree