Skip to content

# Second lvl domain

The tool to check the availability or syntax of domain, IP or URL.

::

    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides the second level domain syntax checker.

Author: Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks: https://pyfunceble.github.io/#/special-thanks

Contributors: https://pyfunceble.github.io/#/contributors

Project link: https://github.com/funilrys/PyFunceble

Project documentation: https://docs.pyfunceble.com

Project homepage: https://pyfunceble.github.io/

License: ::

    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024, 2025 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

## `SecondLvlDomainSyntaxChecker`

Bases: `DomainSyntaxCheckerBase`

Provides an interface to check the syntax of a second domain.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`subject` | `str` | Optional, The subject to work with. | `None`

Source code in `PyFunceble/checker/syntax/second_lvl_domain.py`

     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121

    class SecondLvlDomainSyntaxChecker(DomainSyntaxCheckerBase):
        """
        Provides an interface to check the syntax of a second domain.

        :param str subject:
            Optional, The subject to work with.
        """

        # pylint: disable=line-too-long
        REGEX_VALID_DOMAIN: str = (
            r"^(?=.{0,253}$)(([a-z0-9][a-z0-9-]{0,61}[a-z0-9]|[a-z0-9])\.)+((?=.*[^0-9])([a-z0-9][a-z0-9-]{0,61}[a-z0-9](?:\.)?|[a-z0-9](?:\.)?))$"
        )
        REGEX_VALID_RELAXED_DOMAIN: str = (
            r"^(?=.{0,253}$)(([a-z0-9][a-z0-9_-]{0,61}[a-z0-9_-]|[a-z0-9])\.)+((?=.*[^0-9])([a-z0-9][a-z0-9-]{0,61}[a-z0-9](?:\.)?|[a-z0-9](?:\.)?))$"
        )

        last_point_index: Optional[int] = None
        """
        Saves the index of the last point.
        """

        @DomainSyntaxCheckerBase.ensure_subject_is_given
        def is_valid(self) -> bool:
            """
            Validate the given subject.

            .. warning::
                A valid domain may also be a valid subdomain.

                If you precisely want to check a subdomain please refer to the
                right checker (not this one :-) )!
            """

            # pylint: disable=too-many-return-statements

            extension = self.get_extension()

            if not extension or (
                extension not in self.iana_dataset
                and extension not in self.SPECIAL_USE_DOMAIN_NAMES_EXTENSIONS
            ):
                return False

            subject_without_extension = self.idna_subject[: self.last_point_index]
            subject_without_suffix, _ = self.get_subject_without_suffix(
                self.idna_subject, extension
            )

            if subject_without_suffix:
                if "." in subject_without_suffix:
                    return False

                return self.regex_helper.set_regex(self.REGEX_VALID_DOMAIN).match(
                    self.idna_subject, return_match=False
                ) or self.regex_helper.set_regex(self.REGEX_VALID_RELAXED_DOMAIN).match(
                    self.idna_subject, return_match=False
                )

            if "." in subject_without_extension:
                return False

            return self.regex_helper.set_regex(self.REGEX_VALID_DOMAIN).match(
                self.idna_subject, return_match=False
            )

### `last_point_index = None` `class-attribute` `instance-attribute`

Saves the index of the last point.

### `is_valid()`

Validate the given subject.

.. warning:: A valid domain may also be a valid subdomain.

    If you precisely want to check a subdomain please refer to the
    right checker (not this one :-) )!

Source code in `PyFunceble/checker/syntax/second_lvl_domain.py`

     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121

    @DomainSyntaxCheckerBase.ensure_subject_is_given
    def is_valid(self) -> bool:
        """
        Validate the given subject.

        .. warning::
            A valid domain may also be a valid subdomain.

            If you precisely want to check a subdomain please refer to the
            right checker (not this one :-) )!
        """

        # pylint: disable=too-many-return-statements

        extension = self.get_extension()

        if not extension or (
            extension not in self.iana_dataset
            and extension not in self.SPECIAL_USE_DOMAIN_NAMES_EXTENSIONS
        ):
            return False

        subject_without_extension = self.idna_subject[: self.last_point_index]
        subject_without_suffix, _ = self.get_subject_without_suffix(
            self.idna_subject, extension
        )

        if subject_without_suffix:
            if "." in subject_without_suffix:
                return False

            return self.regex_helper.set_regex(self.REGEX_VALID_DOMAIN).match(
                self.idna_subject, return_match=False
            ) or self.regex_helper.set_regex(self.REGEX_VALID_RELAXED_DOMAIN).match(
                self.idna_subject, return_match=False
            )

        if "." in subject_without_extension:
            return False

        return self.regex_helper.set_regex(self.REGEX_VALID_DOMAIN).match(
            self.idna_subject, return_match=False
        )