import contextlib
import socket


def port_available(port):
    """ Find if a port is in use

    From http://stackoverflow.com/a/35370008

    Args:
        port: The port to be checked.

    Returns:
        True if the port is available; False if it is in use.
    """

    with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
        if sock.connect_ex(("localhost", port)) == 0:
            return False
        else:
            return True