import socket
from contextlib import closing


def is_port_free(port, host="localhost"):
    """Checks if port is open on host"""
    #based on http://stackoverflow.com/a/35370008/952600

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