def is_port_open ( port , host = "127.0.0.1" ) : s = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) try : s . connect ( ( host , int ( port ) ) ) s . shutdown ( 2 ) return True except Exception as e : return False
python check port network connection
Check if a port is open : param port : : param host : : return bool :
def is_port_open(port, host="127.0.0.1"):
    """
    Check if a port is open
    :param port:
    :param host:
    :return bool:
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect((host, int(port)))
        s.shutdown(2)
        return True
    except Exception as e:
        return False