import socket
import time


def check_connect_by_port(port, retry_times=6, sleep_time=0.5):
    """
    Check connect has active with given port.
    
    @param port: Test port.
    @param retry_times: Retry times.
    @param sleep_time: Sleep time between retry, in seconds.
    @return: Return True if given port is active.
    """
    ret_val = False
    test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    retry_time = 0
    while (True):
        try:
            test_socket.connect(("localhost", port))
            ret_val = True
            break
        except socket.error:
            time.sleep(sleep_time)
            retry_time += 1
            if retry_time >= retry_times:
                break
            else:
                continue
    return ret_val